Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fastfib(integer n)
- if n < 0 return 0;
- else if n = 0 return 0;
- else if n = 1 return 1;
- else a ← 1; b ← 0;
- for i from 2 to n do
- t ← a; a ← a + b; b ← t;
- return a;
- end
- (defn fast-fib [n]
- (case n
- 0 0
- 1 1
- (first
- (nth (iterate
- (fn [[a b]]
- (let [t a
- a (+ a b)
- b t]
- [a b]))
- [1 0])
- (- n 1)))))
- (defn fast-fib [n]
- (second
- (nth (iterate
- (fn [[a b]] [(+ a b) a])
- [1 0])
- n)))
- If n ≥ 2, then at the end of the loop the values of a and b will be xxx and yyy.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement