Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function math.Round (x)
- if x >= 0 then
- return math.floor (x + 0.5)
- end -- if positive
- return math.ceil (x - 0.5)
- end -- function round
- function fib(n)
- local x = 0
- local xlast = 1
- for i=1,n,1 do
- x,xlast = x + xlast,x
- end
- return x
- end
- function findPhi()
- local n = 1
- local res
- repeat
- n = n+1
- res = fib(n)/fib(n-1)
- until not (res > 0)
- n = n - 2
- phi = fib(n)/fib(n-1)
- return phi, n
- end
- local phi = findPhi()
- local sqrtFive = math.sqrt(5)
- function fibAlt(n)
- return math.Round(math.pow(((n >= 0) and 1 or -1)*phi, n) / sqrtFive)
- end
- local n = 0
- repeat
- n=n+1
- until(fib(n) ~= fibAlt(n))
- print("Accurate up until and including the "..(n - 1).."th iteration.")
- print("Actual: ",fib(n-1))
- print("Estimate:",fibAlt(n-1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement