Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.08 KB | None | 0 0
  1. ;;; Helper functions:
  2. (defun pairwise-multiply (pair1 pair2)
  3.     (let ((ac (* (first pair1) (first pair2)))
  4.             (ad (* (first pair1) (second pair2)))
  5.             (bc (* (second pair1) (first pair2)))
  6.             (bd (* (second pair1) (second pair2))))
  7.          (list (+ ac ad bc) (+ ac bd))))
  8.  
  9. ;;; If you're going for values well above n=1000000 (and maybe some more),
  10. ;;; you might want to consider making this function tail-recursive, or make
  11. ;;; the iterative version.
  12. (defun pairwise-pow (pair n)
  13.     ;; It's an error having n < 1, you may want to change this to check for
  14.     ;; that condition.
  15.     (cond
  16.         ((= n 1) pair)
  17.         ((evenp n) (let ((result (pairwise-pow pair (truncate (/ n 2)))))
  18.                         (pairwise-multiply result result)))
  19.         (t (let ((result (pairwise-pow pair (truncate (/ n 2)))))
  20.                 (pairwise-multiply pair (pairwise-multiply result result))))))
  21.  
  22. (defun fibonacci (n)
  23.     (if (zerop n) 0
  24.         (first (pairwise-pow '(1 0) n))))
  25.  
  26. ;; Try this one, don't worry, it won't take you a millisecond.
  27. (fibonacci 10000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement