Skip to content

Instantly share code, notes, and snippets.

@brianm
Created February 27, 2009 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianm/71272 to your computer and use it in GitHub Desktop.
Save brianm/71272 to your computer and use it in GitHub Desktop.
proper fibs
let fib n =
let phi = (1 + sqrt 5)/2 in
floor (((phi^n) - ((1-phi)^n)) / (sqrt 5))
#!/usr/bin/env ruby
module Math
PHI = (1 + Math.sqrt(5)) / 2
end
def fib n
(((Math::PHI ** n) - ((1 - Math::PHI) ** n) ) / Math.sqrt(5)).to_i
end
if __FILE__ == $0
puts fib(ARGV[0].to_i)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment