/** A class to test various numeric algorithms for CS 155, Fall 2007, SJSU */ import java.math.BigInteger; import java.util.Arrays; public class A4 { /** A class to test two exponentiation methods, print the results, and print an estimate of the time required @param base the number to be raised to a power @param exp the power to which this number is to be raised */ private static void testPowerMethods(BigInteger base, int exp) { BigInteger power = base.pow(exp); NewNumber.resetClock(); BigInteger naivePower = NewNumber.naivePower(base, exp); System.out.print("for base = " + base + " and "); System.out.println("exponent = " + exp); System.out.print("for naive algorithm, simulated time : "); System.out.println(NewNumber.getClockTime()); if (power.equals(naivePower)) System.out.println("value ok"); else System.out.println("value not ok"); NewNumber.resetClock(); BigInteger fancyPower = NewNumber.fancyPower(base, exp); System.out.print("for base = " + base + " and "); System.out.println("exponent = " + exp); System.out.print("for fancy algorithm, simulated time : "); System.out.println(NewNumber.getClockTime()); if (power.equals(fancyPower)) System.out.println("value ok"); else System.out.println("value not ok"); System.out.println(); } /** A method to test several numeric algorithms. @param args is ignored */ public static void main(String[] args) { // test two algorithms for computing reciprocals mod n System.out.println(NewNumber.newReciprocal(7,1000) + " " + NewNumber.binaryReciprocal(7,1000)); System.out.println(NewNumber.newReciprocal(49,1000) + " " + NewNumber.binaryReciprocal(49,1000)); System.out.println(NewNumber.newReciprocal(246,1000) + " " + NewNumber.binaryReciprocal(246,1000)); System.out.println(NewNumber.newReciprocal(144,233) + " " + NewNumber.binaryReciprocal(144,233)); System.out.println(NewNumber.newReciprocal(233,377) + " " + NewNumber.binaryReciprocal(233,377)); System.out.println(); // Test the Fast Fourier Transform algorithm, as applied // to polynomial multiplication FFT fft = new FFT(); double[] result; result = fft.multiply(new double[] {1, 1, 1, 1}, new double[] {1, 1, 1, 1}); System.out.println(Arrays.toString(result)); result = fft.multiply(new double[] {1, 1, 1, 1}, new double[] {-1, 1, 0, 0}); System.out.println(Arrays.toString(result)); result = fft.multiply(new double[] {-3, 1, 2, 1}, new double[] {1, -1, 0, 2}); System.out.println(Arrays.toString(result)); result = fft.multiply(new double[] {-2, 2, 1, 2}, new double[] {2, -1, 0, 1}); System.out.println(Arrays.toString(result)); // test two exponentiation methods BigInteger two = BigInteger.ONE.add(BigInteger.ONE); testPowerMethods(two, 10); testPowerMethods(two, 512); testPowerMethods(two, 1023); testPowerMethods(two, 1024); System.out.println(); BigInteger three = two.add(BigInteger.ONE); testPowerMethods(three, 10); testPowerMethods(three, 512); for (int i=1024; i<=65536; i=i*2) { testPowerMethods(three, i-1); testPowerMethods(three, i); } } }