-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-19.rkt
31 lines (27 loc) · 841 Bytes
/
1-19.rkt
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
#lang racket
(define (assert-eql actual expected)
(if (not (= expected actual))
(error "Does not match condition, expected: "
expected " actual: " actual)
true))
(define (fib n)
(define (square x) (* x x))
(define (fib-iter a b p q count)
(cond ((= count 0) b)
((even? count)
(fib-iter a
b
(+ (square p) (square q))
(+ (square q) (* 2 p q))
(/ count 2)))
(else (fib-iter (+ (* b q) (* a q) (* a p))
(+ (* b p) (* a q))
p
q
(- count 1)))))
(fib-iter 1 0 0 1 n))
(assert-eql (fib 1) 1)
(assert-eql (fib 2) 1)
(assert-eql (fib 5) 5)
(assert-eql (fib 8) 21)
(assert-eql (fib 20) 6765)