How to resolve the algorithm Pell numbers step by step in the ALGOL 68 programming language
How to resolve the algorithm Pell numbers step by step in the ALGOL 68 programming language
Table of Contents
Problem Statement
Pell numbers are an infinite sequence of integers that comprise the denominators of the closest rational approximations to the square root of 2 but have many other interesting uses and relationships. The numerators of each term of rational approximations to the square root of 2 may also be derived from Pell numbers, or may be found by taking half of each term of the related sequence: Pell-Lucas or Pell-companion numbers.
The Pell numbers: 0, 1, 2, 5, 12, 29, 70, etc., are defined by the recurrence relation: Or, may also be expressed by the closed form formula:
Pell-Lucas or Pell-companion numbers: 2, 2, 6, 14, 34, 82, etc., are defined by a very similar recurrence relation, differing only in the first two terms: Or, may also be expressed by the closed form formula: or
The sequence of rational approximations to the square root of 2 begins: Starting from n = 1, for each term, the denominator is Pn and the numerator is Qn / 2 or Pn-1 + Pn.
Pell primes are Pell numbers that are prime. Pell prime indices are the indices of the primes in the Pell numbers sequence. Every Pell prime index is prime, though not every prime index corresponds to a prime Pell number.
If you take the sum S of the first 4n + 1 Pell numbers, the sum of the terms P2n and P2n + 1 will form the square root of S. For instance, the sum of the Pell numbers up to P5; 0 + 1 + 2 + 5 + 12 + 29 == 49, is the square of P2 + P3 == 2 + 5 == 7. The sequence of numbers formed by the sums P2n + P2n + 1 are known as Newman-Shank-Williams numbers or NSW numbers.
Pell numbers may also be used to find Pythagorean triple near isosceles right triangles; right triangles whose legs differ by exactly 1. E.G.: (3,4,5), (20,21,29), (119,120,169), etc. For n > 0, each right triangle hypotenuse is P2n + 1. The shorter leg length is the sum of the terms up to P2n + 1. The longer leg length is 1 more than that.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pell numbers step by step in the ALGOL 68 programming language
Source code in the algol programming language
BEGIN # find some Pell numbers - trans FreeBASIC ( which is trans Phix ) #
PR read "primes.incl.a68" PR
[ 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 c := 0;
FOR pdx FROM 2 WHILE c < 10 DO
IF is probably prime( p[ pdx ] ) THEN
print( ( whole( pdx, -5 ), " ", whole( p[ pdx ], 0 ), newline ) );
c +:= 1
FI
OD;
print( ( newline, newline, "First 20 Newman-Shank-Williams numbers:", newline ) );
FOR n FROM 0 TO 19 DO
LONG INT nsw = p[ 2 * n ] + p[ 2 * n + 1 ];
print( ( " ", whole( nsw, 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
You may also check:How to resolve the algorithm Bioinformatics/base count step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Map range step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the Elena programming language
You may also check:How to resolve the algorithm Ludic numbers step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Check if a polygon overlaps with a rectangle step by step in the Julia programming language