Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.77 KB | None | 0 0
  1. function math.Round (x)
  2.   if x >= 0 then
  3.     return math.floor (x + 0.5)
  4.   end  -- if positive
  5.  
  6.   return math.ceil (x - 0.5)
  7. end -- function round
  8.  
  9. function fib(n)
  10.     local x = 0
  11.     local xlast = 1
  12.     for i=1,n,1 do
  13.         x,xlast = x + xlast,x
  14.     end
  15.     return x
  16. end
  17.  
  18. function findPhi()
  19.     local n = 1
  20.     local res
  21.     repeat
  22.         n = n+1
  23.         res = fib(n)/fib(n-1)
  24.     until not (res > 0)
  25.     n = n - 2
  26.     phi = fib(n)/fib(n-1)
  27.     return phi, n
  28. end
  29.  
  30. local phi = findPhi()
  31. local sqrtFive = math.sqrt(5)
  32.  
  33. function fibAlt(n)
  34.     return math.Round(math.pow(((n >= 0) and 1 or -1)*phi, n) / sqrtFive)
  35. end
  36.  
  37. local n = 0
  38. repeat
  39.     n=n+1
  40. until(fib(n) ~= fibAlt(n))
  41. print("Accurate up until and including the "..(n - 1).."th iteration.")
  42. print("Actual:  ",fib(n-1))
  43. print("Estimate:",fibAlt(n-1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement