Created
April 27, 2010 20:16
-
-
Save skreuzer/381259 to your computer and use it in GitHub Desktop.
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
// O(1) in both time and space | |
unsigned fibonacci(unsigned n) | |
{ | |
double s5 = sqrt(5.0); | |
double phi = (1.0 + s5) / 2.0; | |
double left = pow(phi, (double)n); | |
double right = pow(1.0-phi, (double)n); | |
return (unsigned)((left - right) / s5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment