Pell numbers: Difference between revisions

Added (partially) the Common Lisp version
(Added (partially) the Common Lisp version)
Line 94:
 
 
 
==={{header|FreeBASIC}}===
=={{header|Common Lisp}}==
 
<syntaxhighlight lang="CommonLisp">
(defun recurrent-sequence-2 (a0 a1 k1 k2 max)
"A generic function for any recurrent sequence of order 2, where a0 and a1 are the initial elements,
k1 is the factor of a(n-1) and k2 is the factor of a(n-2)"
(do* ((i 0 (1+ i))
(result (list a1 a0))
(b0 a0 b1)
(b1 a1 b2)
(b2 (+ (* k1 b1) (* k2 b0)) (+ (* k1 b1) (* k2 b0))) )
((> i max) (nreverse result))
(push b2 result) ))
 
(defun pell-sequence (max)
(recurrent-sequence-2 0 1 2 1 max) )
 
(defun pell-lucas-sequence (max)
(recurrent-sequence-2 2 2 2 1 max) )
 
(defun fibonacci-sequence (max) ; As an extra goody, you get Fibonacci's numbers with this simple call
(recurrent-sequence-2 1 1 1 1 max) )
 
 
 
(defun rational-approximation-sqrt2 (max)
"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))) )
</syntaxhighlight>
 
 
The first 10 Pell numbers are:
<syntaxhighlight lang="CommonLisp">
(pell-sequence 10)
(0 1 2 5 12 29 70 169 408 985 2378 5741 13860)
</syntaxhighlight>
 
The first 10 Pell-Lucas numbers are:
<syntaxhighlight lang="CommonLisp">
(pell-lucas-sequence 10)
(2 2 6 14 34 82 198 478 1154 2786 6726 16238 39202)
</syntaxhighlight>
 
Rational approximation of square root of 2:
<syntaxhighlight lang="CommonLisp">
(rational-approximation-sqrt2 10)
(1 3/2 7/5 17/12 41/29 99/70 239/169 577/408 1393/985 3363/2378 8119/5741
19601/13860)
</syntaxhighlight>
 
The same in decimal form:
<syntaxhighlight lang="CommonLisp">
(mapcar #'float (rational-approximation-sqrt2 10))
(1.0 1.5 1.4 1.4166666 1.4137931 1.4142857 1.4142011 1.4142157 1.4142132
1.4142137 1.4142135 1.4142135)
</syntaxhighlight>
 
 
Still missing some of the taks
 
 
 
 
 
 
 
 
 
 
==={{header|FreeBASIC}}===
{{trans|Phix}}
<syntaxhighlight lang="freebasic">#define isOdd(a) (((a) and 1) <> 0)
5

edits