Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write each of the following recursive methods and for each one, write a main

ID: 3918878 • Letter: 1

Question

1. Write each of the following recursive methods and for each one, write a main method to test it.
public static int sumCubes(int n):Returns the sum of the sequence: 13 + 23 + 33+ …. + n3

public static double betterPower(double x, int n) :This is an improved version of the power method that takes advantage of the following properties: x n = xn/2 * xn/2 for even values of n x n = x * xn/2 * xn/2 for odd values of n

public static int GCD(int n, int m) :Returns the Greatest Common Divisor (GCD) of two numbers n and m, define as follows:

if ,11 11 and 11 mod m = 0 GCD(n, m ) = if n

Explanation / Answer

public class power {

public static void main(String args[]) {

System.out.print(sumCubes(5));

System.out.println(power_value(4,5));

System.out.println(GCD(24,16));

}

public static int sumCubes(int n) {

if(n==0)return 0;

return (n*10)+3+sumCubes(n-1);

}

public static double power_value(double x,int n) {

if(n==0)return 1;

if(n%2==0)return power_value(x,n/2)*power_value(x,n/2);

return x*power_value(x,n/2)*power_value(x,n/2);

}

public static int GCD(int a,int b) {

if(a%b==0)return b;

if(b%a==0)return a;

if(a>b)return GCD(a%b,b);

return GCD(a,b%a);

}

}

for any query please comment.

please upvote if find it helpful.

Thank you.