Theres a big bug there!, you need 2 "start" variables, otherwise the time is wrong.
Heres my Nim solution:
import times var fibo_cache = newSeq[int](200) func recursive_fibonacci(num: int): int = if num in {0, 1}: return num result = recursive_fibonacci(num - 1) + recursive_fibonacci(num - 2) proc dynamic_fibonacci(num: int): int = if num in {0, 1}: return num elif 0 != fibo_cache[num]: return fibo_cache[num] fibo_cache[num] = dynamic_fibonacci(num - 1) + dynamic_fibonacci(num - 2) result = fibo_cache[num] when isMainModule: const fibo_num = 45 # change as needed let start = now() echo "Dynamic fibonacci answer = ", dynamic_fibonacci(fibo_num) echo "Time for Dynamic = ", now() - start let start2 = now() echo "Recursive fibonacci answer = ", recursive_fibonacci(fibo_num) echo "Time for Recursive = ", now() - start2
I use let start2 = now() to fix the bug.
let start2 = now()
Also dynamic_fibonacci() has Side-Effects but recursive_fibonacci() does not.
dynamic_fibonacci()
recursive_fibonacci()
Fixed, thanks :-)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Theres a big bug there!, you need 2 "start" variables, otherwise the time is wrong.
Heres my Nim solution:
I use
let start2 = now()
to fix the bug.Also
dynamic_fibonacci()
has Side-Effects butrecursive_fibonacci()
does not.Fixed, thanks :-)