Pell numbers: Difference between revisions
Content added Content deleted
(Added (partially) the Common Lisp version) |
|||
Line 115: | Line 115: | ||
(recurrent-sequence-2 2 2 2 1 max) ) |
(recurrent-sequence-2 2 2 2 1 max) ) |
||
(defun fibonacci-sequence (max) ; As an extra |
(defun fibonacci-sequence (max) ; As an extra bonus, you get Fibonacci's numbers with this simple call |
||
(recurrent-sequence-2 1 1 1 1 max) ) |
(recurrent-sequence-2 1 1 1 1 max) ) |
||
(defun rational-approximation-sqrt2 (max) |
(defun rational-approximation-sqrt2 (max) |
||
"Approximate square root of 2 with (P(n-1)+P(n-2))/P(n)" |
"Approximate square root of 2 with (P(n-1)+P(n-2))/P(n)" |
||
(butlast (maplist #'(lambda (l) (/ (+ (first l) (or (second l) 0)) (or (second l) 1))) (pell-sequence max))) ) |
(butlast (maplist #'(lambda (l) (/ (+ (first l) (or (second l) 0)) (or (second l) 1))) (pell-sequence max))) ) |
||
(defun pell-primes (max) |
|||
(do* ((i 0 (1+ i)) |
|||
(result (list 1 0)) |
|||
(b0 0 b1) |
|||
(b1 1 b2) |
|||
(b2 (+ (* 2 b1) (* 1 b0)) (+ (* 2 b1) (* 1 b0))) ) |
|||
((> (length result) max) (nreverse result)) |
|||
; primep can be any function determining whether a number is prime, for example, |
|||
; https://rosettacode.org/wiki/Primality_by_Wilson%27s_theorem#Common_Lisp |
|||
(when (primep b2) |
|||
(push b2 result) ))) |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||
Line 152: | Line 163: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
First 7 Pell primes: |
|||
<syntaxhighlight lang="CommonLisp"> |
|||
(pell-primes 7) |
|||
(0 1 2 5 29 5741 33461 44560482149) |
|||
</syntaxhighlight> |
|||
Still missing some of the taks |
Still missing some of the taks |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |