Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn fibs []
- ((fn rfib [a b]
- (lazy-seq (cons a (rfib b (+' a b)))))
- 0 1))
- (time (do (nth (fibs) 10000) nil))
- "Elapsed time: 6.386237 msecs"
- nil
- ;;;; A java version that takes approx. 30ms
- public class NthFib {
- private static BigInteger nthFib(int n) {
- BigInteger a = BigInteger.ZERO;
- BigInteger b = BigInteger.ONE;
- BigInteger z;
- for (int i = 1; i < n; i++) {
- z = b;
- b = a.add(z);
- a = z;
- }
- return b;
- }
- public static void main(String[] args) {
- long s = System.currentTimeMillis();
- BigInteger f = nthFib(10000);
- long e = System.currentTimeMillis();
- System.out.println("Took " + (e - s) + "ms.");
- System.out.println(f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement