Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Test your algorithms solutions in Java at http://codingbat.com/prob/p158888 Compare the results of your three solutions down below and assess which does more or less work.

Test your algorithms solutions in Java at http://codingbat.com/prob/p158888

Compare the results of your three solutions down below and assess which does more or less work.

1.Develop a transform-and-conquer algorithm for the calculation. Describe your idea used in the transformation. Evaluate your algorithm to determine the number of multiplications performed overall.

Solution# 1

public int powerN(int base, int n) {

if (n == 0) return 1;

if (n % 2 == 0) return powerN(base,n/2)*powerN(base,n/2);

else return base*powerN(base,n/2)*powerN(base,n/2);

}

----------------------------------------------------------------------------------------------------

2.Present a brute-force algorithm for the calculation and show the number of multiplications performed overall.

Solution# 2

public int powerN(int base, int n) {

if (n == 0) return 1;

return base*powerN(base,n-1);

}

-----------------------------------------------------------------------------------------------------

3.Develop a recursive reduce-by-one algorithm for the calculation, establish and solve the recurrence relation for the number of multiplications performed overall.

Solution# 3

public int powerN(int base, int n) { // at the end return n == 0 return 1 if (n == 0) { return 1; } else { return base * powerN(base, n - 1); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Robb,Carlos Coronel

5th Edition

061906269X, 9780619062699

More Books

Students also viewed these Databases questions