mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-22 21:35:03 +00:00
fibonacci improvements
This commit is contained in:
parent
9209b8dd92
commit
475ddf5a20
@ -4,8 +4,9 @@
|
|||||||
(def slowFib (fn (n) (if (< n 2) n (+ (slowFib (- n 1)) (slowFib (- n 2))))))
|
(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
|
;; 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
|
;; 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 fibListRec (fn (n l) (if (< (len l) n) (fibListRec n (push l (sumOfLast l))) l)))
|
||||||
(def fibList (fn (n) (fibListRec n [0 1])))
|
(def fibList (fn (n) (fibListRec n [0 1])))
|
||||||
|
(def fastFib (fn (n) (sumOfLast (fibList n))))
|
||||||
|
Loading…
Reference in New Issue
Block a user