// Name:     Bradley Shedd
// Program:  Project#4
// Date:     November 16, 2010
// Class:    Java1302
// Filename: Fibo.java
// Purpose:  This class writes a recursive and an iterative function to
//           calculate the 40th element in a Fibonacci sequence. It also
//           times the process in milliseconds.

public class Fibo
{
   
public static void main(String args[])
    {
     
long start = System.currentTimeMillis();
      System.out.println(
"-----------------------------------------------" +
                        
"-----------------------------------------------");
      System.out.println(
"The 40th element in this Fibonacci sequence using" +
                        
" the recursive funtion is " + recursiveFib(40));
     
long end = System.currentTimeMillis();
      System.out.println(
"The time past in milliseconds(msec) to return the " +
                        
"element recursively is " + (end-start));
      start = System.currentTimeMillis();
      System.out.println(
"-----------------------------------------------" +
                        
"-----------------------------------------------");
      System.out.println(
"The 40th element using the iterative function" +
                        
" is " + iterativeFib(40));
      end = System.currentTimeMillis();
      System.out.println(
"The time past in msec to return the element " +
                        
"iteratively is " + (end-start));
      System.out.println(
"===============================================" +
                         
"===============================================");
     
for
         (int i = 1; i <=40; i++)
      System.out.println(iterativeFib(i));
      System.out.println(
"===============================================" +
                        
"===============================================");
     
    }




public static int recursiveFib(int n)
{
 
if (n<2)
  
return n;
 
 
else
  
return recursiveFib(n-1)+recursiveFib(n-2);

}
public static int iterativeFib(int n)
{
int i,x = 0, y = 1;
 
if (n<2)
  
return n;
 
 
else
   for (i=1;i<n;i++)
   {
     y=x+y;
     x=y-x;
   }
  
  
return y;
   }
}

Homepage