Created
December 18, 2014 15:58
-
-
Save soltrinox/2ce9f4e35ee58bffef03 to your computer and use it in GitHub Desktop.
Fibonacci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int fibonacci(int num){ //non-recursive fibonacci. returns the nth fibonacci number. | |
double phi= (1+Math.sqrt(5))/2; | |
return((int)((Math.pow(phi,num)-Math.pow(1-phi,num))/Math.sqrt(5))); //finding fibonnaci number using formula. | |
} | |
public static int fibonacciRec(int num){ //recurive fibonacci. Used to demonstrate fibonacci | |
if(num==1||num==2){ //stopping case. F(1)=1, F(2)=1 by definition. | |
return(1); | |
} | |
return(fibonacciRec(num-1)+fibonacciRec(num-2)); //definition of Fibonacci sequence: F(n)=F(n-1)+F(n-2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment