[ create a new paste ] login | about

Link: http://codepad.org/90DQYRBD    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Sep 30:
; lucas sequences

(define (lucas l2 l1 n)
  (let loop ((n n) (l2 l2) (l1 l1) (ls (list l1 l2)))
    (if (zero? n) (reverse ls)
      (let ((l (+ l1 l2)))
        (loop (- n 1) l1 l (cons l ls))))))

(display (lucas 1 1 20)) (newline) ; fibonacci numbers
(display (lucas 1 3 20)) (newline) ; lucas numbers

(define (lucas p q l2 l1 n)
  (let loop ((n n) (l2 l2) (l1 l1) (ls (list l1 l2)))
    (if (zero? n) (reverse ls)
      (let ((l (- (* p l1) (* q l2))))
        (loop (- n 1) l1 l (cons l ls))))))

(display (lucas 1 -1 1 1 20)) (newline) ; fibonacci numbers
(display (lucas 1 -1 1 3 20)) (newline) ; lucas numbers

(define (u n p q)
  (if (< n 1) 1 (if (= n 1) p
    (let ((k (quotient n 2)))
      (if (odd? n)
          (- (* (u (+ k 1) p q) (v k p q)) (expt q k))
          (* (u k p q) (v k p q)))))))

(define (v n p q)
  (if (< n 1) 2 (if (= n 1) p
    (let ((k (quotient n 2)))
      (if (odd? n)
          (- (* (v (+ k 1) p q) (v k p q)) (* p (expt q k)))
          (- (expt (v k p q) 2) (* 2 (expt q k))))))))

(display (u 22 1 -1)) (newline) ; fibonacci
(display (v 22 1 -1)) (newline) ; lucas


Output:
1
2
3
4
5
6
(1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711)
(1 3 4 7 11 18 29 47 76 123 199 322 521 843 1364 2207 3571 5778 9349 15127 24476 39603)
(1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711)
(1 3 4 7 11 18 29 47 76 123 199 322 521 843 1364 2207 3571 5778 9349 15127 24476 39603)
17711
39603


Create a new paste based on this one


Comments: