Question
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:
-
It prompts the user to enter the base of a power.
-
It prompts the user to enter the exponent of a power.
-
It computes the powers using both the fast and naive algorithms and displays the powers.
-
It randomly generates a four-digit positive integer for the base of a power.
-
It randomly generates a two-digit positive integer for the exponent of a power.
-
Similarly, it computes the powers using these randomly generated in- tegers and both the fast and naive algorithms and displays the powers.
-
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.
-
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:
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 8192Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started