Skip to content

Instantly share code, notes, and snippets.

@preprocessor
Last active December 25, 2015 12:49
Show Gist options
  • Save preprocessor/6979752 to your computer and use it in GitHub Desktop.
Save preprocessor/6979752 to your computer and use it in GitHub Desktop.
nth Fibbonacci number in lua
function floor(n)
return math.floor(n)
end
function sqrt(n)
return math.sqrt(n)
end
Phi = (1+sqrt(5))/2
phi = (1-sqrt(5))/2
--^ because I'm lazy and i like clean code^
function fib(n)
n = floor(n+0.5) --rounds the input
return floor(((Phi^n)-(-phi^n))/sqrt(5))
end
--[[
Usage: fib(int)
> Fib(10):
55
>
--]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment