- Engineering
- Computer Science
- this program is in java compiled in the eclipse ide...
Question: this program is in java compiled in the eclipse ide...
Question details
**This program is in JAVA, compiled in the Eclipse IDE.
Question: This is a program that compares naive and fast algorithms. The code compiles without error for me, but I am noticing an output error on the output line "Using Fast Algorithm" (pictured below, above code). For some reason, instead of performing the calculation, it just spits out the base input. Could you please review the code below and tell me where my mistake might be so I can fix this?
import java.math.BigInteger;
import java.util.Random;
import java.util.Scanner;
/**
* Purpose: A program that compares naive and fast algorithms
* @author caseycampbell,
BRIKISH(online tutor)
* @since 01/27/1997
* @references powertimes.xlsx,
https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html
* proj00s19StarterCodeJava,
https://moodle3.lsu.edu/pluginfile.php/2069923/mod_resource/content/52/csc3102proj00js19.pdf,
*
https://moodle3.lsu.edu/pluginfile.php/2069924/mod_resource/content/22/ProgrammingTipsJava01.txt
*/
// Check line 109
// Add decimal method to condense to four spots
// Condense files
// Check outputs, format outputs
public class powerAnalyzer
{
public static void main(String args[])
{
Scanner sc = new
Scanner(System.in);
//Prompts user to enter the base of
a power
System.out.print("Enter the base of
the power: ");
BigInteger base =
BigInteger.valueOf(sc.nextLong());
// Prompts user to enter exponent
of a power
System.out.print("Enter the
exponent of the power: ");
int exponent = sc.nextInt();
//Compute the powers using fast and
naive algorithms
// Calls for method naivePow of
class BigMath
BigInteger result =
BigMath.naivePow(base,exponent);
// Prints the result
System.out.println("Using Naive
Algorithm: " + base + "^" + exponent + " = " + result);
// Calls for method fastPow
of class BigMath
result = BigMath.fastPow1(base,
exponent);
// Prints the result
System.out.println("Using Fast
Algorithm: " + base + "^" + exponent + " = " + result);
// Using randomly generated
numbers, defines the Random generator
Random rand = new
Random();
//Generate four digit
positive integer for the bases
int rBase = rand.nextInt(10000) +
1000;
// Converts 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.fastPow1(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.fastPow1(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);
}
}
public static class BigMath{
public static BigInteger
naivePow(BigInteger base, int n) throws
IllegalArgumentException
{
BigInteger
result = BigInteger.ONE;
//Loops through
till the values of n are reached
for (int i = 0;
i < n; i++)
{
result= result.multiply(base);
}
return
result;
}
public static BigInteger
fastPow1(BigInteger base, int exponent) {
return
base;
}
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
fastPow1(fastPow1(base, n / 2), 2);
else
return
base.multiply(fastPow1(fastPow1(base, (n - 1) / 2), 2));
}
}
}
Solution by an expert tutor
