-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.scm
70 lines (50 loc) · 1.34 KB
/
fibonacci.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(use-modules (Scheme+))
;; (fibonacci 13)
;; 7
(define (fibonacci n)
(if (< n 2)
n
(+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
;; (fib 11)
;; 89
(define (fib n)
(if {n < 2}
n
{(fib {n - 1}) + (fib {n - 2})} ))
;; (fib2 27)
;; 196418
(define (fib2 n)
(if {n < 2}
n
{ fib2({n - 1}) + fib2({n - 2}) }
))
;;(fib3 47)
;; 2971215073 ;; but it takes time in minutes...
(define (fib3 n)
(if {n < 2}
n
{ fib3{n - 1} + fib3{n - 2} }
))
(define size0 10000)
(define memo0 (make-vector size0 0))
;; example:
;; scheme@(guile-user)> (fibdyna0 57)
;; 365435296162
(define (fibdyna0 n)
(cond ((< n 2) n)
((not (zero? (vector-ref memo0 n))) (vector-ref memo0 n))
(else (let ((fibn (+ (fibdyna0 (- n 1)) (fibdyna0 (- n 2)))))
(vector-set! memo0 n fibn)
fibn))))
;; cheme@(guile-user)> (fibdyna 27)
;; 196418
;; scheme@(guile-user)> (fibdyna 47)
;; 2971215073 ; immediate result, no more take times
;; scheme@(guile-user)> (fibdyna 257)
;; 229265413057075367692743352179590077832064383222590237
{size <+ 10000}
{memo <+ (make-vector size 0)}
(define (fibdyna n)
(cond ({n < 2} n)
({memo[n] <> 0} {memo[n]}) ;; and not Nemo who is a fish ;-) do not make typos as me :-O or you get: Unbound variable: nemo :-)
(else {memo[n] <- {(fibdyna {n - 1}) + (fibdyna {n - 2})}}) ))