Pell numbers: Difference between revisions
Content added Content deleted
(add RPL) |
(Added Algol 68) |
||
Line 94: | Line 94: | ||
=={{header|ALGOL 68}}== |
|||
{{Trans|FreeBASIC|using Millar Rabin for finding Pell primes and some minor output format differences and showing 10 Pell primes}} |
|||
Assumes LONG INT is large enough to hold the 90th Pell number (as in Algol 68G, versions 2 and 3). |
|||
{{libheader|ALGOL 68-primes}} |
|||
<syntaxhighlight lang="algol68"> |
|||
BEGIN # find some Pell numbers - trans FreeBASIC ( which is trans Phix ) # |
|||
PR read "primes.incl.a68" PR |
|||
PROC is prime = ( LONG INT v )BOOL: |
|||
IF v < 2 THEN FALSE |
|||
ELIF v MOD 2 = 0 THEN v = 2 |
|||
ELIF v MOD 3 = 0 THEN v = 3 |
|||
ELSE |
|||
LONG INT d := 5; |
|||
BOOL result := TRUE; |
|||
WHILE result AND d * d <= v DO |
|||
IF v MOD d = 0 |
|||
THEN result := FALSE |
|||
ELSE |
|||
d +:= 2; |
|||
IF v MOD d = 0 |
|||
THEN result := FALSE |
|||
ELSE d +:= 4 |
|||
FI |
|||
FI |
|||
OD; |
|||
result |
|||
FI # is prime # ; |
|||
INT n; |
|||
[ 0 : 90 ]LONG INT p, pl; |
|||
p[ 0 ] := 0; p[ 1 ] := 1; |
|||
pl[ 0 ] := 2; pl[ 1 ] := 2; |
|||
FOR n FROM 2 TO UPB p DO |
|||
p[ n ] := 2 * p[ n - 1 ] + p[ n - 2 ]; |
|||
pl[ n ] := 2 * pl[ n - 1 ] + pl[ n - 2 ] |
|||
OD; |
|||
print( ( "First 20 Pell numbers:", newline ) ); |
|||
FOR n FROM 0 TO 19 DO print( ( " ", whole( p[ n ], 0 ) ) ) OD; |
|||
print( ( newline, newline, "First 20 Pell-Lucas numbers:", newline ) ); |
|||
FOR n FROM 0 TO 19 DO print( ( " ", whole( pl[ n ], 0 ) ) ) OD; |
|||
print( ( newline, newline, "First 20 rational approximations of sqrt(2) (" ) ); |
|||
print( ( fixed( sqrt( 2 ), -15, 13 ), "):", newline ) ); |
|||
FOR n TO 20 DO |
|||
LONG INT j = pl[ n ] OVER 2, d = p[ n ]; |
|||
print( ( " ", whole( j, 0 ), "/", whole( d, 0 ), " ~= ", fixed( j / d, -15, 13 ), newline ) ) |
|||
OD; |
|||
print( ( newline, "First 10 Pell primes:", newline, "index Pell prime", newline ) ); |
|||
INT pdx := 2; |
|||
INT c := 0; |
|||
WHILE |
|||
IF is probably prime( p[ pdx ] ) THEN |
|||
print( ( whole( pdx, -5 ), " ", whole( p[ pdx ], 0 ), newline ) ); |
|||
c +:= 1 |
|||
FI; |
|||
pdx +:= 1; |
|||
c < 10 |
|||
DO SKIP OD; |
|||
[ 0 : 20 ]LONG INT nsw; |
|||
FOR n FROM 0 TO 19 DO nsw[ n ] := p[ 2 * n ] + p[ 2 * n + 1 ] OD; |
|||
print( ( newline, newline, "First 20 Newman-Shank-Williams numbers:", newline ) ); |
|||
FOR n FROM 0 TO 19 DO print( ( " ", whole( nsw[ n ], 0 ) ) ); IF n = 13 THEN print( ( newline ) ) FI OD; |
|||
print( ( newline, newline, "First 20 near isosceles right triangles:", newline ) ); |
|||
LONG INT i0 := 0, i1 := 1, t := 1, found := 0; |
|||
FOR i FROM 2 WHILE found < 20 DO |
|||
LONG INT i2 = i1*2 + i0; |
|||
IF ODD i THEN |
|||
print( ( " [", whole( t, 0 ), ", ", whole( t + 1, 0 ), ", ", whole( i2, 0 ), "]", newline ) ); |
|||
found +:= 1 |
|||
FI; |
|||
t +:= i2; i0 := i1; i1 := i2 |
|||
OD |
|||
END |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre style="height:40ex;overflow:scroll;"> |
|||
First 20 Pell numbers: |
|||
0 1 2 5 12 29 70 169 408 985 2378 5741 13860 33461 80782 195025 470832 1136689 2744210 6625109 |
|||
First 20 Pell-Lucas numbers: |
|||
2 2 6 14 34 82 198 478 1154 2786 6726 16238 39202 94642 228486 551614 1331714 3215042 7761798 18738638 |
|||
First 20 rational approximations of sqrt(2) (1.4142135623731): |
|||
1/1 ~= 1.0000000000000 |
|||
3/2 ~= 1.5000000000000 |
|||
7/5 ~= 1.4000000000000 |
|||
17/12 ~= 1.4166666666667 |
|||
41/29 ~= 1.4137931034483 |
|||
99/70 ~= 1.4142857142857 |
|||
239/169 ~= 1.4142011834320 |
|||
577/408 ~= 1.4142156862745 |
|||
1393/985 ~= 1.4142131979695 |
|||
3363/2378 ~= 1.4142136248949 |
|||
8119/5741 ~= 1.4142135516461 |
|||
19601/13860 ~= 1.4142135642136 |
|||
47321/33461 ~= 1.4142135620573 |
|||
114243/80782 ~= 1.4142135624273 |
|||
275807/195025 ~= 1.4142135623638 |
|||
665857/470832 ~= 1.4142135623747 |
|||
1607521/1136689 ~= 1.4142135623728 |
|||
3880899/2744210 ~= 1.4142135623731 |
|||
9369319/6625109 ~= 1.4142135623731 |
|||
22619537/15994428 ~= 1.4142135623731 |
|||
First 10 Pell primes: |
|||
index Pell prime |
|||
2 2 |
|||
3 5 |
|||
5 29 |
|||
11 5741 |
|||
13 33461 |
|||
29 44560482149 |
|||
41 1746860020068409 |
|||
53 68480406462161287469 |
|||
59 13558774610046711780701 |
|||
89 4125636888562548868221559797461449 |
|||
First 20 Newman-Shank-Williams numbers: |
|||
1 7 41 239 1393 8119 47321 275807 1607521 9369319 54608393 318281039 1855077841 10812186007 |
|||
63018038201 367296043199 2140758220993 12477253282759 72722761475561 423859315570607 |
|||
First 20 near isosceles right triangles: |
|||
[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] |
|||
[159140519, 159140520, 225058681] |
|||
[927538920, 927538921, 1311738121] |
|||
[5406093003, 5406093004, 7645370045] |
|||
[31509019100, 31509019101, 44560482149] |
|||
[183648021599, 183648021600, 259717522849] |
|||
[1070379110496, 1070379110497, 1513744654945] |
|||
[6238626641379, 6238626641380, 8822750406821] |
|||
[36361380737780, 36361380737781, 51422757785981] |
|||
[211929657785303, 211929657785304, 299713796309065] |
|||
[1235216565974040, 1235216565974041, 1746860020068409] |
|||
</pre> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |