From 475ddf5a20704c7d7ba0edd715cef7d69cb1c8af Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Sat, 17 Oct 2020 14:15:14 +0700 Subject: [PATCH] fibonacci improvements --- mad/fib.mad | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mad/fib.mad b/mad/fib.mad index f093016..5318a14 100644 --- a/mad/fib.mad +++ b/mad/fib.mad @@ -4,8 +4,9 @@ (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))))) +(def sumOfLast (fn (l) (+ (last l) (penult l)))) ;; Faster version, return vector of n numbers, tail call optimized (def fibListRec (fn (n l) (if (< (len l) n) (fibListRec n (push l (sumOfLast l))) l))) (def fibList (fn (n) (fibListRec n [0 1]))) +(def fastFib (fn (n) (sumOfLast (fibList n))))