RosettaCodeData/Task/Fibonacci-sequence/NewLISP/fibonacci-sequence-4.l

16 lines
473 B
Text
Raw Permalink Normal View History

2026-04-30 12:34:36 -04:00
;;; Global variable (bigints); can be isolated in a namespace if need be
2023-07-01 11:58:00 -04:00
(setq stack '(0L 1L))
;
2026-04-30 12:34:36 -04:00
;;; If the stack is too short, complete it; then read from it
;;; Adding at the end of a list is optimized in NewLisp
2023-07-01 11:58:00 -04:00
(define (fib n)
2026-04-30 12:34:36 -04:00
(while (<= (length stack) n)
(push (+ (stack -1) (stack -2)) stack -1))
(stack n))
2023-07-01 11:58:00 -04:00
;
;;; Test (~ 7+ s on my mediocre laptop)
;(println (time (fib 50000)))
2026-04-30 12:34:36 -04:00
;;; or
2023-07-01 11:58:00 -04:00
(println (length (fib 50000)))
2026-04-30 12:34:36 -04:00
;;; outputs 10450 (digits)