Pell numbers: Difference between revisions

J
m (typo)
(J)
Line 89:
;* [[wp:Pythagorean_triple|Wikipedia: Pythagorean_triple]]
 
 
 
 
=={{header|J}}==
 
As detailed in the task description, there's a variety of ways to compute these values.
 
For example:
 
<lang J>nextPell=: , 1 2+/ .*_2&{. NB. pell, list extender
Pn=: (%:8) %~(1+%:2)&^ - (1-%:2)&^ NB. pell, closed form
Qn=: (1+%:2)&^ + (1-%:2)&^ NB. pell lucas, closed form
QN=: +: %&Pn ] NB. pell lucas, closed form
qn=: 2 * (+&Pn <:) NB. pell lucas, closed form</lang>
 
Thus:
 
<lang J> nextPell^:9(0 1)
0 1 2 5 12 29 70 169 408 985 2378
Pn i.11
0 1 2 5 12 29 70 169 408 985 2378
nextPell^:9(2 2)
2 2 6 14 34 82 198 478 1154 2786 6726
Qn i.11
2 2 6 14 34 82 198 478 1154 2786 6726
QN i.11
0 2 6 14 34 82 198 478 1154 2786 6726
qn i.11
2 2 6 14 34 82 198 478 1154 2786 6726</lang>
 
QN (P<sub>2n</sub> / P<sub>n</sub>) doesn't get the first element of the pell lucas sequence right. We could fix this by changing the definition:
 
<lang J>QN=: 2 >. +: %&Pn ]
QN i.11
2 2 6 14 34 82 198 478 1154 2786 6726</lang>
 
Continuing... the first ten rational approximations to √2 here would be:
<lang J> }.(% _1}. +//.@,:~) nextPell^:9(0 1)
1 0.666667 0.714286 0.705882 0.707317 0.707071 0.707113 0.707106 0.707107 0.707107
}.(% _1}. +//.@,:~) nextPell^:9(0 1x)
1 2r3 5r7 12r17 29r41 70r99 169r239 408r577 985r1393 2378r3363</lang>
 
The first ten pell primes are:
<lang J> 10{.(#~ 1&p:)nextPell^:99(0 1x)
2 5 29 5741 33461 44560482149 1746860020068409 68480406462161287469 13558774610046711780701 4125636888562548868221559797461449</lang>
 
Their indices are:
<lang J> 10{.I. 1&p:nextPell^:99(0 1x)
2 3 5 11 13 29 41 53 59 89</lang>
 
The NSW numbers are the running sums of the sums of (non-overlapping) pairs of pell numbers, or:
<lang J> +/\_2 +/\ nextPell^:20(0 1x)
1 8 49 288 1681 9800 57121 332928 1940449 11309768 65918161</lang>
 
The first ten pell based pythogorean triples would be:
<lang J> }.(21$1 0)#|:(}.,~0 1+/+/\@}:)nextPell^:(20)0 1
3 4 5
20 21 29
119 120 169
696 697 985
4059 4060 5741
23660 23661 33461
137903 137904 195025
803760 803761 1136689
4684659 4684660 6625109
27304196 27304197 38613965</lang>
 
 
6,951

edits