Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. fastfib(integer n)
  2. if n < 0 return 0;
  3. else if n = 0 return 0;
  4. else if n = 1 return 1;
  5. else a ← 1; b ← 0;
  6. for i from 2 to n do
  7. t ← a; a ← a + b; b ← t;
  8. return a;
  9. end
  10.  
  11. (defn fast-fib [n]
  12. (case n
  13. 0 0
  14. 1 1
  15. (first
  16. (nth (iterate
  17. (fn [[a b]]
  18. (let [t a
  19. a (+ a b)
  20. b t]
  21. [a b]))
  22. [1 0])
  23. (- n 1)))))
  24.  
  25. (defn fast-fib [n]
  26. (second
  27. (nth (iterate
  28. (fn [[a b]] [(+ a b) a])
  29. [1 0])
  30. n)))
  31.  
  32. 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