Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

LANGUAGE: JAVA Long story short, I have to write a program that compares the fast power algorithm and the naive power algorithm in java, using

LANGUAGE: JAVA

Long story short, I have to write a program that compares the fast power algorithm and the naive power algorithm in java, using the BigMath class and PowerAnalyzer class. It has to do these eight things:

The PowerAnalyzer class will consist of only one method, the main. The main method will perform the following tasks:

  1. It prompts the user to enter the base of a power.

  2. It prompts the user to enter the exponent of a power.

  3. It computes the powers using both the fast and naive algorithms and displays the powers.

  4. It randomly generates a four-digit positive integer for the base of a power.

  5. It randomly generates a two-digit positive integer for the exponent of a power.

  6. Similarly, it computes the powers using these randomly generated in- tegers and both the fast and naive algorithms and displays the powers.

  7. It prompts the user for a positive integer to be used as the base in measuring and displaying runtimes for generating various powers of the base.

  8. Forn={16,32,64,128,256,512,1024,2048,4096,8192},yourprogram will compute, using the naive and fast exponentiation algorithms, pow- ers of the base entered by the user and measure and display the exe- cution times in nanoseconds for these algorithms as shown in the table in the sample run.

I have been given code where it has been started, and I am to complete the methods and fill out the program such that it works without bugs. The code cannot be manipulated other than filling in methods and functions. This is the code.

import poweranalyzer; import java.math.BigInteger;

/* * Provides implementations for a naive and a fast power method for * BigIntegers types with integer exponents * @author *****, YOUR NAME * @since 99/99/9999 * @see java.math.BigInteger */

public class BigMath { /** * This method computes the non-negative power of a non-negative "BigInteger" * for a given non-negative integer exponent using repeated multiplication; * given a base, b, and the exponent n, the function uses n-1 multiplications * to compute b * b * b * ..... * b, where b is used as a factor n times. * @param base a non-negative object of the BigInt class * @param n a non-negative integer * @return the power of a big integer to the specified non-negative exponent * @throw IllegalArgumentExcepiton when n is negative or base is not positive. */ public static BigInteger naivePow(BigInteger base, int n) throws IllegalArgumentException { //Implement this method }

/** * This method computes the non-negative power of a positive "BigInteger" * using the fast power algorithm that uses successive squaring * @param base a positive object of the BigInt class * @param n a non-negative integer * @return the power of a big integer to the specified non-negative exponent * @throw IllegalArgumentException when n is negative or base is not positive. */ public static BigInteger fastPow(BigInteger base, int n) throws IllegalArgumentException { //Implement this method } }

The end result must look something like this:

image text in transcribed

Please help me fill in the methods such that it looks similar to this. I am struggling with some of the code.

Sample Run: Enter the base of the power -> 2 Enter the exponent of the power -> 100 Using Naive Algorithm: 2*100 1267650600228229401496703205376 Using Fast Algorithm: 2'100 1267650600228229401496703205376 Using a random 4-digit base and a random 2-digit exponent: Using Naive Algorithm: 6362 24 - Using Fast Algorithm: 6362 24 - Enter the base of the powers for the table ->546879 b = 546879 bn: Fast Power(ns) bn: Naive Power (ns) 16 32 64 128 256 512 1024 2048 4096 8192

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

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions

Question

What is e-commerce.

Answered: 1 week ago

Question

Differentiate tan(7x+9x-2.5)

Answered: 1 week ago

Question

Explain the sources of recruitment.

Answered: 1 week ago

Question

Differentiate sin(5x+2)

Answered: 1 week ago

Question

Compute the derivative f(x)=1/ax+bx

Answered: 1 week ago

Question

8-6 Who poses the biggest security threat: insiders or outsiders?

Answered: 1 week ago