Question: JAVA CODE I NEED HELP WITH QUESTION 3 THATS BASED OFF THE SECOND PICTURE QUESTION THAT I ALREADY DID. I ADDED THE CODES BELOW. THANK

JAVA CODE

I NEED HELP WITH QUESTION 3 THATS BASED OFF THE SECOND PICTURE QUESTION THAT I ALREADY DID. I ADDED THE CODES BELOW. THANK YOU!

JAVA CODE I NEED HELP WITH QUESTION 3 THATS BASED OFF THE

SECOND PICTURE QUESTION THAT I ALREADY DID. I ADDED THE CODES BELOW.

POWER 1

public class power1 {

private static int count;

private static int power1(int x, int n)

{

// initalize results

int ans = 1;

if (n >= 0)

{

// x by n times

for (int i = 0; i

ans = ans * x;

}

}

return ans;

}

public static void main(String[] args) {

int x = 3;

int n = 19;

System.out.print("The number of multiplications are = ");

System.out.print(n - 1);

System.out.print(" ");

System.out.print("The power is: " + power1(x, n));

}

}

POWER 2

public class power2 {

private static int count = 0;

private static int recursion = 0;

public static int[] power1(int x, int n) {

int power = x; // storing the count

int count = 0;

// Run a for loop n-1 times and multiply with power

for (int i = 1; i

power *= x;

count++;

}

return new int[] { power, count };

}

// method 1 of recursion

public static int power2(int x, int n) {

if (0 == n) {

return 1;

}

count++;

recursion++;

return x * power2(x, n - 1);

}

// method 2 of recursion

public static void main(String[] args) {

System.out.println("for [EVEN] 3 power 32 = " + power2(3, 32) +

", Number of multiplications = " + count +

" Number of recursive calls = " + recursion++);

count = 0;

recursion = 0;

System.out.println("for [ODD] 3 power 19 = " + power2(3, 19) +

", Number of multiplications = " + count +

" Number of recursive calls = " + recursion++);

}

}

POWER 3

public class power3 {

private static int count = 0;

private static int recursion = 0;

public static int power3(int x, int n) {

if (0 == n) {

return 1;

}

// If n is even

if (0 == n % 2) {

recursion++;

int temp = power3(x, n / 2);

count++;

return temp * temp;

} else { // n is odd

recursion++;

int temp = power3(x, n / 2);

count += 2;

return x * temp * temp;

}

}

public static void main(String[] args) {

recursion = 0;

System.out.println("for [EVEN] 3 power 32 = " + power3(3, 32) +

", Number of multiplications = " + count +

" Number of recursive calls = " + recursion++);

count = 0;

recursion = 0;

System.out.println("for [ODD] 3 power 19 = " + power3(3, 19) +

", Number of multiplications = " + count +

" Number of recursive calls = " + recursion++);

}

}

3. (10 pts) There are three methods to compute x in Exercise 18 of Chapter 3 from the textbook. a) What is the execution time of power1(x, n) in Big O notation? Also, justify your answer. b) What is the execution time of power2(x, n) in Big O notation? Also, justify your answer. c) What is the execution time of power3(x, n) in Big O notation? Also, justify your answer. d) Which one is the most efficient? * This problem considers several ways to compute x" for some 20. 2. Write an iterative method power) to compute x* for 20. b. Write a recursive method power2 to compute x" by using the following recur- site formulation: x-xx-1 if x > 0 c. Write a recursive method power3 to compute x* by using the following recur sive formulation: 0.1 {"18W/2, if n > 0 and n is even - 3. (4/22 ifn > 0 and is odd 2. How many multiplications will each of the methods powerl, power2, and power perform when computing 332 319 c. How many recursive calls will power and power3 make when computing 332, 319

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!