Advertisement
Stekeblad

Fibonacci sequence algorithms comparison recursive vs loop

Nov 12th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.48 KB | None | 0 0
  1. --[[ Copyright 2017 Stekeblad
  2. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  3.  
  4. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  5.  
  6. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  7.  
  8. -------------------------------------------------------------------------------------------------------------------------
  9.  
  10. This is a small script that compares two fibonacci sequence algorithms.
  11. One is the classic school example of a recursive algorithm and the second one is one I wrote that is using a loop instead.
  12.  
  13. Here is the results I got after some testing:
  14. The slow version starts to slow down at about fibN = 35 (2 seconds) and increases fast (15 seconds at 40, 30 minutes at 50)
  15. The fast one finishes in under a second for fibN = 200, the problem here is the variables overflow and starts to return negative results after 161
  16.  
  17. ]]
  18. local arg = {...}
  19.  
  20. if tonumber(arg[1]) then
  21.   fibN = tonumber(arg[1])
  22.  else
  23.   error("Invalid parameter, give witch fibonacci number ot calculate")
  24.  end
  25.  
  26. function fib(n)
  27.   if n==0 then return 0 end
  28.   if n==1 then return 1 end
  29.   return fib(n-1) + fib(n-2)
  30. end
  31.  
  32. function fib2(n)
  33.   if n==0 then return 0 end
  34.   if n==1 then return 1 end
  35.  
  36.   local v1, v2, v3, m = 0, 1, 1, 1
  37.   while (m < n) do
  38.     v3 = v2 + v1
  39.     v1 = v2
  40.     v2 = v3
  41.     m = m + 1
  42.   end
  43.   return v3
  44. end
  45.  
  46.  
  47. print ("calculating " .. fibN .."th fibonacci number..")
  48.  
  49. local start = os.time()
  50. print("Fast version: " ..fib2(fibN))
  51. print("time: " .. os.time() - start)
  52.  
  53. if arg[2] then
  54.   print("Skipping slow version.")
  55. else
  56.   start = os.time()
  57.   print("Slow version: " .. fib(fibN))
  58.   print("time: " .. os.time() - start)
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement