From fd5e846f2fa8a29d9a000428c348926d70ba381d Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Sat, 6 Jun 2020 15:59:05 +0700 Subject: [PATCH] Fibonacci numbers --- mad/fib.mad | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 mad/fib.mad diff --git a/mad/fib.mad b/mad/fib.mad new file mode 100644 index 0000000..61eb631 --- /dev/null +++ b/mad/fib.mad @@ -0,0 +1,11 @@ +; Functions to calculate Fibonacci numbers + +; Slow recursive version +(def slowFib (fn (n) (if (< n 2) n (+ (slowFib (- n 1)) (slowFib (- n 2)))))) + +; Return the sum of the last 2 numbers in a sequence +(def sumOfLast (fn (l) (+ (last l) (get l (- (len l) 2))))) + +; Faster version, return vector of n numbers +(def fibListRec (fn (n l) (if (< (len l) n) (fibListRec n (push l (sumOfLast l))) l))) +(def fibList (fn (n) (fibListRec n [0 1])))