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
Related
larzmathSmall math helpers→FizzBuzz in LarzscriptThe classic interview warm-up: print 1 to N, but Fizz for multiples of 3, Buzz for multiples of 5, FizzBuzz for both.→Reverse a String in LarzscriptFlip a string end to end - one of the smallest real string algorithms, and a good first look at the str package.→Check if a String is a Palindrome in LarzscriptDetect whether a string reads the same forwards and backwards, ignoring case, spaces and punctuation.→