/* File: FibBasic.java - March 2011 */ package l04; /** * Compute and display the Fibonacci number of the input argument * using a recursive algorithm * * Sample usage: java l04.Fibbasic 10 * * Note, this is intended to illustrate a problem with recursion. * Due to the double call to fib within fib, this method becomes very * slow for inputs on the order of 40, and essentially useless for inputs * of 50 or more. * * This can be fixed iteratively, or using a more subtle recursive approach, * see other code in this package * * @author Michael Albert * */ public class FibBasic{ public static void main(String[] args) { int n = Integer.parseInt(args[0]); System.out.println("Fib(" + n + ") = " + fib(n)); } public static int fib(int n) { if (n <= 1) return 1; return fib(n-1) + fib(n-2); } }