import java.math.BigInteger; import java.util.Scanner; /** * Replace this JavaDoc comment for the purpose of this class */ public class Solution { /** * Gets data from standard input */ private Scanner input = null; /** * Start the execution of the solution * @param args Command line arguments */ public static void main(String args[]) { Solution solution = new Solution(); solution.run(); } // /** * Run the solution. This method is called from main() */ public void run() { // Create object to read data from standard input this.input = new Scanner(System.in); // This code replicates the input to the standard output // Modify this code to solve the problem while ( this.input.hasNextLong() ) { BigInteger base = this.input.nextBigInteger(); long exponent = this.input.nextLong(); System.out.printf("%,d^%,d: %,d%n", base, exponent, recursivePower(base, exponent)); System.out.printf("%,d^%,d: %,d%n", base, exponent, efficientTailRecursivePower(base, exponent)); } // Close the standard input this.input.close(); } public static BigInteger iterativeFactorial(long number) { BigInteger result = BigInteger.valueOf(1); for ( long current = 1; current <= number; ++current ) result = result.multiply( BigInteger.valueOf(current) ); return result; } public static BigInteger recursivePower(BigInteger base, long exponent) { if ( exponent <= 0 ) return BigInteger.valueOf(1); else return base.multiply( recursivePower(base, exponent - 1) ); } public static BigInteger efficientRecursivePower(BigInteger base, long exponent) { if ( exponent <= 0 ) return BigInteger.valueOf(1); else if ( exponent == 1 ) return base; else if ( exponent % 2 == 0 ) return efficientRecursivePower(base.multiply(base), exponent / 2); else return base.multiply( efficientRecursivePower(base, exponent - 1) ); } public static BigInteger efficientTailRecursivePower(BigInteger base, long exponent) { return efficientTailRecursivePower(base, exponent, BigInteger.valueOf(1)); } public static BigInteger efficientTailRecursivePower(BigInteger base, long exponent, BigInteger result) { if ( exponent <= 0 ) return result; else if ( exponent % 2 == 0 ) return efficientTailRecursivePower(base.multiply(base), exponent / 2, result); else return efficientTailRecursivePower(base, exponent - 1, base.multiply(result) ); } // }