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() ) { long number = this.input.nextLong(); System.out.printf("%,d: %,d%n", number, recursiveFibonacci(number)); System.out.printf("%,d: %,d%n%n", number, tailRecursiveFibonacci(number)); } // 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 recursiveFibonacci(long number) { if ( number <= 1 ) return BigInteger.valueOf(number); else return recursiveFibonacci(number - 1).add( recursiveFibonacci(number - 2) ); } public static BigInteger tailRecursiveFibonacci(long number) { return tailRecursiveFibonacci(number, BigInteger.valueOf(0), BigInteger.valueOf(1)); } public static BigInteger tailRecursiveFibonacci(long number, BigInteger previous, BigInteger result) { if ( number <= 0 ) return previous; else if ( number == 1 ) return result; else return tailRecursiveFibonacci(number - 1, result, previous.add(result) ); } // }