Created
December 15, 2015 13:07
-
-
Save Nata01/b95ab865f22fc3954d17 to your computer and use it in GitHub Desktop.
Fibonacci numbers. Simplest algorithm.
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
int fibSimple(int elem){ | |
if(elem < 0){ | |
return 0; | |
} | |
if(elem == 0){ | |
return 0; | |
} | |
if(elem == 1){ | |
return 1; | |
} | |
else{ | |
int value; | |
int prev = 0; | |
int cur = 1; | |
for(int i = 2; i <= elem; i++){ | |
value = prev + cur; | |
prev = cur; | |
cur = value; | |
} | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment