Pell numbers: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 96:
==={{header|FreeBASIC}}===
{{trans|Phix}}
<langsyntaxhighlight lang="freebasic">#define isOdd(a) (((a) and 1) <> 0)
 
Function isPrime(Byval ValorEval As Integer) As Boolean
Line 165:
i += 1
Loop
Sleep</langsyntaxhighlight>
{{out}}
<pre style="height:40ex;overflow:scroll;">First 20 Pell numbers:
Line 235:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 308:
u1 = u2
}
}</langsyntaxhighlight>
 
{{out}}
Line 391:
For example:
 
<langsyntaxhighlight Jlang="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</langsyntaxhighlight>
 
Thus:
 
<langsyntaxhighlight Jlang="j"> nextPell^:9(0 1)
0 1 2 5 12 29 70 169 408 985 2378
Pn i.11
Line 410:
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</langsyntaxhighlight>
 
QN (which is defined as 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:
 
<langsyntaxhighlight Jlang="j">QN=: 2 >. +: %&Pn ]
QN i.11
2 2 6 14 34 82 198 478 1154 2786 6726</langsyntaxhighlight>
 
Continuing... the first ten rational approximations to √2 here would be:
<langsyntaxhighlight Jlang="j"> }.(%~ _1}. +//.@,:~) nextPell^:9(0 1)
1 1.5 1.4 1.41667 1.41379 1.41429 1.4142 1.41422 1.41421 1.41421
}.(%~ _1}. +//.@,:~) nextPell^:9(0 1x)
1 3r2 7r5 17r12 41r29 99r70 239r169 577r408 1393r985 3363r2378</langsyntaxhighlight>
 
The first ten pell primes are:
<langsyntaxhighlight Jlang="j"> 10{.(#~ 1&p:)nextPell^:99(0 1x)
2 5 29 5741 33461 44560482149 1746860020068409 68480406462161287469 13558774610046711780701 4125636888562548868221559797461449</langsyntaxhighlight>
 
Their indices are:
<langsyntaxhighlight Jlang="j"> 10{.I. 1&p:nextPell^:99(0 1x)
2 3 5 11 13 29 41 53 59 89</langsyntaxhighlight>
 
The NSW numbers are the sums of (non-overlapping) pairs of pell numbers, or:
<langsyntaxhighlight Jlang="j"> _2 +/\ nextPell^:20(0 1x)
1 7 41 239 1393 8119 47321 275807 1607521 9369319 54608393
</syntaxhighlight>
</lang>
 
The first ten pell based pythogorean triples would be:
<langsyntaxhighlight Jlang="j"> }.(21$1 0)#|:(}.,~0 1+/+/\@}:)nextPell^:(20)0 1
3 4 5
20 21 29
Line 448:
803760 803761 1136689
4684659 4684660 6625109
27304196 27304197 38613965</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function pellnumbers(wanted)
Line 517:
printrows("Twenty Newman-Shank-Williams numbers:", newmanshankwilliams(20), 17, 5)
printrows("Twenty near isosceles triangle triplets:", nearisosceles(20), 52, 2)
</langsyntaxhighlight>{{out}}
<pre>
Twenty Pell numbers:
Line 579:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[PellNumber, PellLucasNumber]
PellNumber[0] = 0;
PellNumber[1] = 1;
Line 611:
{short, long, hypo}
]
PythagoreanTriple /@ Range[10]</langsyntaxhighlight>
{{out}}
<pre>{0, 1, 2, 5, 12, 29, 70, 169, 408, 985}
Line 638:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <state say>;
Line 680:
my $x = sum @pell[0..$y-1];
printf "(%d, %d, %d)\n", $x, $x+1, $pell[$y]
} 1..$upto;</langsyntaxhighlight>
{{out}}
<pre style="height:20ex">First 20 Pell numbers:
Line 761:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
Line 802:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"[%d, %d, %d]\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">side</span><span style="color: #0000FF;">,</span><span style="color: #000000;">side</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hypot</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 881:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>my $pell = cache lazy 0, 1, * + * × 2 … *;
my $pell-lucas = lazy 2, 2, * + * × 2 … *;
 
Line 900:
 
say "\nFirst $upto near isosceles right triangles:";
map -> \p { printf "(%d, %d, %d)\n", |($_, $_+1 given $pell[^(2 × p + 1)].sum), $pell[2 × p + 1] }, 1..$upto;</langsyntaxhighlight>
{{out}}
<pre style="height:40ex;overflow:scroll;">First 20 Pell numbers:
Line 981:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">val pellNumbers: LazyList[BigInt] =
BigInt("0") #:: BigInt("1") #::
(pellNumbers zip pellNumbers.tail).
Line 1,043:
println(pellTriple.take(10).mkString("7. Near Right-triangle Triples: \n\n", "\n", "\n"))
}
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:48ex;overflow:scroll;>7 Tasks
Line 1,109:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "./big" for BigInt, BigRat
import "./math" for Int
import "./fmt" for Fmt
Line 1,174:
p1 = p2
i = i + 1
}</langsyntaxhighlight>
 
{{out}}
10,333

edits