Advertisement
Guest User

Untitled

a guest
Sep 13th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. (defn fibs []
  2. ((fn rfib [a b]
  3. (lazy-seq (cons a (rfib b (+' a b)))))
  4. 0 1))
  5.  
  6. (time (do (nth (fibs) 10000) nil))
  7. "Elapsed time: 6.386237 msecs"
  8. nil
  9.  
  10. ;;;; A java version that takes approx. 30ms
  11.  
  12. public class NthFib {
  13. private static BigInteger nthFib(int n) {
  14. BigInteger a = BigInteger.ZERO;
  15. BigInteger b = BigInteger.ONE;
  16. BigInteger z;
  17. for (int i = 1; i < n; i++) {
  18. z = b;
  19. b = a.add(z);
  20. a = z;
  21. }
  22. return b;
  23. }
  24.  
  25. public static void main(String[] args) {
  26. long s = System.currentTimeMillis();
  27. BigInteger f = nthFib(10000);
  28. long e = System.currentTimeMillis();
  29. System.out.println("Took " + (e - s) + "ms.");
  30. System.out.println(f);
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement