Question
I can't get this code to compile, and I can't figure out where the errors are. It's in java, and I've tried everything I can
I can't get this code to compile, and I can't figure out where the errors are. It's in java, and I've tried everything I can think of to make it work. Please help me?
import java.math.BigInteger; import java.util.Random; import java.util.Scanner;
public class PowerAnalyzer { // Driver method main public static void main(String args[]) { // Define the Scanner Scanner sc = new Scanner(System.in);
// 1. Prompt the user to enter the base of a power System.out.print("Enter the base of the power -> "); BigInteger base = BigInteger.valueOf(sc.nextLong());
// 2. Prompt the user to enter exponent of a power System.out.print("Enter the exponent of the power -> "); int exponent = sc.nextInt();
// 3. Compute the powers using fast and naive algorithms // Call for method naivePow of class BigMath BigInteger result = BigMath.naivePow(base, exponent);
// Print the result System.out.println("Using Naive Algorithm: " + base + "^" + exponent + " = " + result);
// Call for method fastPow of class BigMath result = BigMath.fastPow(base, exponent);
// Print the result System.out.println("Using Fast Algorithm: " + base + "^" + exponent + " = " + result);
// Using randomly generated numbers // Define the Random generator Random rand = new Random();
// 4. Generate four digit positive integer for the base int rBase = rand.nextInt(10000) + 1000; // Convert the generated number to BigInteger BigInteger rBigBase = BigInteger.valueOf(rBase);
// 5. Randomly generate a two digit positive integer int rExponent = rand.nextInt(100) + 10;
// 6. Compute the powers and display the results System.out.println(""); System.out.println("Using a random 4-digit base and a random 2-digit exponent: "); System.out.println( "Using Naive Algorithm: " + rBigBase + "^" + rExponent + " = " + BigMath.naivePow(rBigBase, rExponent)); System.out.println( "Using Fast Algorithm: " + rBigBase + "^" + rExponent + " = " + BigMath.fastPow(rBigBase, rExponent));
// 7. Prompt the user for a positive integer to be used as the base System.out.print("Enter the base of the powers for the table -> "); BigInteger tBase = BigInteger.valueOf(sc.nextInt());
// 8. Define the array n, for exponents int n[] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
// Calculate and print the time consumed in the form of table System.out.println("b = " + tBase); System.out.println("=================================================="); System.out.println("n b^n: Fast Power(ns) b^n: Naive Power (ns)");
// Loop through each value of n for (int i = 0; i < n.length; i++) { // Calculate the time consumed for fast algorithm long startTime = System.nanoTime(); BigMath.fastPow(tBase, n[i]); long endTime = System.nanoTime(); long timeFast = endTime - startTime;
// Calculate the time consumed for naive algorithm startTime = System.nanoTime(); BigMath.naivePow(tBase, n[i]); endTime = System.nanoTime(); long timeNaive = endTime - startTime;
// Print the results System.out.println(n[i] + " " + timeFast + " " + timeNaive);
} } }
//BigMath.java
import java.math.BigInteger;
/* * Provides implementations for a naive and a fast power method for * BigIntegers types with integer exponents */ 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 { // Define BigInteger for result BigInteger result = BigInteger.ONE;
// Loop through till the values of n reached for (int i = 0; i < n; i++) { // Calculate the exponent result = result.multiply(base); }
// Return the result return result; }
/** * 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 { // Recursive call for calculating power using fast algorithm if (n == 1) return base;
if (n == 2) return base.multiply(base);
if (n % 2 == 0) return fastPow(fastPow(base, n / 2), 2);
else return base.multiply(fastPow(fastPow(base, (n - 1) / 2), 2)); } }
Step 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