Skip to content

Instantly share code, notes, and snippets.

@skreuzer
Created April 27, 2010 20:16
Show Gist options
  • Save skreuzer/381259 to your computer and use it in GitHub Desktop.
Save skreuzer/381259 to your computer and use it in GitHub Desktop.
// 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