Pell numbers: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Changed to Wren S/H) |
(Added Oberon-07) |
||
Line 1,409: | Line 1,409: | ||
(4684659, 4684660, 6625109) |
(4684659, 4684660, 6625109) |
||
(27304196, 27304197, 38613965) |
(27304196, 27304197, 38613965) |
||
</pre> |
|||
=={{header|Oberon-07}}== |
|||
{{Trans|ALGOL_W|which is a translation of FreeBASIC which is a translation of Phix}} |
|||
<syntaxhighlight lang="modula2"> |
|||
MODULE PellNumbers; (* find some Pell numbersl trans Algol W, FreeBasic, Phix *) |
|||
IMPORT Out, Math; |
|||
CONST MAXP = 9; |
|||
VAR p, pl :ARRAY 21 OF INTEGER; (* need more than 10 Pell numbers *) |
|||
c, pdx, j, n, nsw :INTEGER; (* to find the fifth Pell prime *) |
|||
(* returns true if n is prime, false otherwise, uses trial division *) |
|||
PROCEDURE isPrime( n : INTEGER ):BOOLEAN; |
|||
VAR prime :BOOLEAN; |
|||
f, f2, toNext :INTEGER; |
|||
BEGIN |
|||
IF n < 3 THEN prime := n = 2 |
|||
ELSIF n MOD 3 = 0 THEN prime := n = 3 |
|||
ELSIF ~ ODD( n ) THEN prime := FALSE |
|||
ELSE |
|||
prime := TRUE; |
|||
f := 5; |
|||
f2 := 25; |
|||
toNext := 24; (* note: ( 2n + 1 )^2 - ( 2n - 1 )^2 = 8n *) |
|||
WHILE ( f2 <= n ) & prime DO |
|||
prime := n MOD f # 0; |
|||
f := f + 2; |
|||
f2 := toNext; |
|||
toNext := toNext + 8 |
|||
END |
|||
END |
|||
RETURN prime |
|||
END isPrime; |
|||
PROCEDURE NearIsoscelesRightTriangles; |
|||
VAR i, i0, i1, i2, t, found :INTEGER; |
|||
BEGIN |
|||
i0 := 0; i1 := 1; t := 1; found := 0; |
|||
i := 1; |
|||
WHILE found < 10 DO |
|||
i := i + 1; |
|||
i2 := i1*2 + i0; |
|||
IF ODD( i ) THEN |
|||
Out.String( " [" );Out.Int( t, 0 ); |
|||
Out.String( ", " );Out.Int( t + 1, 0 ); |
|||
Out.String( ", " );Out.Int( i2, 0 ); |
|||
Out.String( "]" );Out.Ln; |
|||
found := found + 1 |
|||
END; |
|||
t := i2; i0 := i1; i1 := i2 |
|||
END |
|||
END NearIsoscelesRightTriangles; |
|||
BEGIN |
|||
p[ 0 ] := 0; p[ 1 ] := 1; |
|||
pl[ 0 ] := 2; pl[ 1 ] := 2; |
|||
FOR n := 2 TO 20 DO |
|||
p[ n ] := 2 * p[ n - 1 ] + p[ n - 2 ]; |
|||
pl[ n ] := 2 * pl[ n - 1 ] + pl[ n - 2 ] |
|||
END; |
|||
Out.String( "First 10 Pell numbers:" ); |
|||
FOR n := 0 TO MAXP DO Out.String( " " );Out.Int( p[ n ], 1 ) END; Out.Ln; |
|||
Out.String( "First 10 Pell-Lucas numbers:" ); |
|||
FOR n := 0 TO MAXP DO Out.String( " " );Out.Int( pl[ n ], 1 ) END; Out.Ln; |
|||
Out.Ln;Out.String( "First 10 rational approximations of sqrt(2) (" ); |
|||
Out.Real( Math.sqrt( 2.0 ), 8 );Out.String( "):" ); |
|||
FOR n := 1 TO MAXP DO |
|||
j := pl[ n ] DIV 2; |
|||
Out.Ln; |
|||
Out.String( " " );Out.Int( j, 0 );Out.String( "/" );Out.Int( p[ n ], 0 ); |
|||
Out.String( " ~= " );Out.Real( FLT( j ) / FLT( p[ n ] ), 8 ); |
|||
END; |
|||
Out.Ln; |
|||
Out.Ln;Out.String( "First 5 Pell primes:" ); |
|||
Out.Ln;Out.String( "index Pell prime" ); |
|||
c := 0; |
|||
pdx := 2; |
|||
WHILE c < 5 DO |
|||
IF isPrime( p[ pdx ] ) THEN |
|||
Out.Ln;Out.Int( pdx, 6 );Out.String( " " );Out.Int( p[ pdx ], 0 ); |
|||
c := c + 1 |
|||
END; |
|||
pdx := pdx + 1 |
|||
END; |
|||
Out.Ln;Out.Ln;Out.String( "First 10 Newman-Shank-Williams numbers:" );Out.Ln; |
|||
FOR n := 0 TO MAXP DO |
|||
nsw := p[ 2 * n ] + p[ 2 * n + 1 ]; |
|||
Out.String( " " );Out.Int( nsw, 0 ) |
|||
END; |
|||
Out.Ln; |
|||
Out.Ln;Out.String( "First 10 near isosceles right triangles:" );Out.Ln; |
|||
NearIsoscelesRightTriangles |
|||
END PellNumbers. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First 10 Pell numbers: 0 1 2 5 12 29 70 169 408 985 |
|||
First 10 Pell-Lucas numbers: 2 2 6 14 34 82 198 478 1154 2786 |
|||
First 10 rational approximations of sqrt(2) (1.414214): |
|||
1/1 ~= 1.000000 |
|||
3/2 ~= 1.500000 |
|||
7/5 ~= 1.400000 |
|||
17/12 ~= 1.416667 |
|||
41/29 ~= 1.413793 |
|||
99/70 ~= 1.414286 |
|||
239/169 ~= 1.414201 |
|||
577/408 ~= 1.414216 |
|||
1393/985 ~= 1.414213 |
|||
First 5 Pell primes: |
|||
index Pell prime |
|||
2 2 |
|||
3 5 |
|||
5 29 |
|||
11 5741 |
|||
13 33461 |
|||
First 10 Newman-Shank-Williams numbers: |
|||
1 7 41 239 1393 8119 47321 275807 1607521 9369319 |
|||
First 10 near isosceles right triangles: |
|||
[2, 3, 5] |
|||
[12, 13, 29] |
|||
[70, 71, 169] |
|||
[408, 409, 985] |
|||
[2378, 2379, 5741] |
|||
[13860, 13861, 33461] |
|||
[80782, 80783, 195025] |
|||
[470832, 470833, 1136689] |
|||
[2744210, 2744211, 6625109] |
|||
[15994428, 15994429, 38613965] |
|||
</pre> |
</pre> |
||