Algorithms · real, tested code

Generate Fibonacci Numbers in Larzscript

Compute the Nth Fibonacci number - iteratively, so it stays fast even for large N.

mathx.fib() uses an O(n) iterative loop (two running variables), not naive recursion, so fib(50) is instant instead of exponential.

larzscript pkg install mathx

Code

import "mathx" as mathx

let i = 0
while i < 10 {
  print(mathx.fib(i))
  i = i + 1
}
print("fib(50) =", mathx.fib(50))
Output
0
1
1
2
3
5
8
13
21
34
fib(50) = 12586269025