Pell numbers: Difference between revisions

→‎{{header|Oberon-07}}: Fix the near Isosoeles triangles
(→‎{{header|Oberon-07}}: Fix the near Isosoeles triangles)
 
(5 intermediate revisions by 4 users not shown)
Line 321:
<pre style="height:40ex;overflow:scroll;">
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 Pell-Lucas numbers: 2 2 6 14 34 82 198 478 1154 2786
First 10 rational approximations of sqrt(2) (1.414213):
First 10 rational approximations of sqrt(2) (1.414214):
1/1 ~= 1.000000
3/2 ~= 1.500000
7/5 ~= 1.400000
17/12 ~= 1.416666416667
41/29 ~= 1.413793
99/70 ~= 1.414285414286
239/169 ~= 1.414201
577/408 ~= 1.414215414216
1393/985 ~= 1.414213
 
Line 346:
 
First 10 near isosceles right triangles:
[23, 34, 5]
[1220, 1321, 29]
[70119, 71120, 169]
[408696, 409697, 985]
[23784059, 23794060, 5741]
[1386023660, 1386123661, 33461]
[80782137903, 80783137904, 195025]
[470832803760, 470833803761, 1136689]
[27442104684659, 27442114684660, 6625109]
[1599442827304196, 1599442927304197, 38613965]
</pre>
 
Line 888:
4684659 4684660 6625109
27304196 27304197 38613965</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.util.ArrayList;
import java.util.List;
 
public final class PellNumbers {
 
public static void main(String[] aArgs) {
System.out.println("7 Tasks");
System.out.println("-------");
System.out.println("Task 1, Pell Numbers: " + pellNumbers(TERM_COUNT));
System.out.println();
System.out.println("Task 2, Pell-Lucas Numbers: " + pellLucasNumbers(TERM_COUNT));
System.out.println();
System.out.println("Task 3, Approximations to square root of 2:");
List<Rational> rationals = squareRoot2(TERM_COUNT + 1);
for ( int i = 1; i < TERM_COUNT + 1; i++ ) {
System.out.println(rationals.get(i) + " = " + rationals.get(i).toBigDecimal());
}
System.out.println();
List<Pair> pairs = pellPrimes(TERM_COUNT);
System.out.println("Task 4, Pell primes:");
for ( int i = 0; i < TERM_COUNT; i++ ) {
System.out.println(pairs.get(i).pellPrime);
}
System.out.println();
System.out.print("Task 5, Pell indices of Pell primes:");
for ( int i = 0; i < TERM_COUNT; i++ ) {
System.out.print(pairs.get(i).index + " ");
}
System.out.println();
System.out.println();
System.out.println("Task 6, Newman-Shank-Williams numbers: " + newmanShankWilliams(TERM_COUNT));
System.out.println();
System.out.println("Task , Near isoseles right triangles:");
List<Triple> nearIsoselesRightTriangles = nearIsoslesRightTriangles(TERM_COUNT);
for ( int i = 0; i < TERM_COUNT; i++ ) {
System.out.println(nearIsoselesRightTriangles.get(i));
}
}
private static List<BigInteger> pellNumbers(int aTermCount) {
PellIterator pellIterator = new PellIterator(BigInteger.ZERO, BigInteger.ONE);
List<BigInteger> result = new ArrayList<BigInteger>();
for ( int i = 0; i < aTermCount; i++ ) {
result.add(pellIterator.next());
}
return result;
}
private static List<BigInteger> pellLucasNumbers(int aTermCount) {
PellIterator pellLucasIterator = new PellIterator(BigInteger.TWO, BigInteger.TWO);
List<BigInteger> result = new ArrayList<BigInteger>();
for ( int i = 0; i < aTermCount; i++ ) {
result.add(pellLucasIterator.next());
}
return result;
}
private static List<Rational> squareRoot2(int aTermCount) {
PellIterator pellIterator = new PellIterator(BigInteger.ZERO, BigInteger.ONE);
PellIterator pellLucasIterator = new PellIterator(BigInteger.TWO, BigInteger.TWO);
List<Rational> result = new ArrayList<Rational>();
for ( int i = 0; i < aTermCount; i++ ) {
result.add( new Rational(pellLucasIterator.next().divide(BigInteger.TWO), pellIterator.next()) );
}
return result;
}
private static List<Pair> pellPrimes(int aTermCount) {
PellIterator pellIterator = new PellIterator(BigInteger.ZERO, BigInteger.ONE);
int index = 0;
int count = 0;
List<Pair> result = new ArrayList<Pair>();
while ( count < aTermCount ) {
BigInteger pellNumber = pellIterator.next();
if ( pellNumber.isProbablePrime(16) ) {
result.add( new Pair(pellNumber, index) );
count += 1;
}
index += 1;
}
return result;
}
private static List<BigInteger> newmanShankWilliams(int aTermCount) {
PellIterator pellIterator = new PellIterator(BigInteger.ZERO, BigInteger.ONE);
List<BigInteger> result = new ArrayList<BigInteger>();
for ( int i = 0; i < aTermCount; i++ ) {
BigInteger pellNumber = pellIterator.next();
result.add(pellNumber.add(pellIterator.next()));
}
return result;
}
private static List<Triple> nearIsoslesRightTriangles(int aTermCount) {
PellIterator pellIterator = new PellIterator(BigInteger.ZERO, BigInteger.ONE);
pellIterator.next();
List<Triple> result = new ArrayList<Triple>();
BigInteger sum = pellIterator.next();
for ( int i = 0; i < aTermCount; i++ ) {
sum = sum.add(pellIterator.next());
BigInteger nextTerm = pellIterator.next();
result.add( new Triple(sum, sum.add(BigInteger.ONE), nextTerm) );
sum = sum.add(nextTerm);
}
return result;
}
private static class PellIterator {
public PellIterator(BigInteger aFirst, BigInteger aSecond) {
a = aFirst; b = aSecond;
}
public BigInteger next() {
aCopy = a;
bCopy = b;
b = b.add(b).add(a);
a = bCopy;
return aCopy;
}
private BigInteger a, aCopy, b, bCopy;
}
private static record Rational(BigInteger numerator, BigInteger denominator) {
public BigDecimal toBigDecimal() {
return new BigDecimal(numerator).divide( new BigDecimal(denominator), mathContext );
}
public String toString() {
return numerator + " / " + denominator;
}
private static MathContext mathContext = new MathContext(34);
}
private static record Pair(BigInteger pellPrime, int index) {}
private static record Triple(BigInteger shortSide, BigInteger longSide, BigInteger hypotenuse) {
public String toString() {
return "(" + shortSide + ", " + longSide + ", " + hypotenuse + ")" ;
}
}
private static final int TERM_COUNT = 10;
}
</syntaxhighlight>
{{ out }}
<pre>
7 Tasks
-------
Task 1, Pell Numbers: [0, 1, 2, 5, 12, 29, 70, 169, 408, 985]
 
Task 2, Pell-Lucas Numbers: [2, 2, 6, 14, 34, 82, 198, 478, 1154, 2786]
 
Task 3, Approximations to square root of 2:
1 / 1 = 1
3 / 2 = 1.5
7 / 5 = 1.4
17 / 12 = 1.416666666666666666666666666666667
41 / 29 = 1.413793103448275862068965517241379
99 / 70 = 1.414285714285714285714285714285714
239 / 169 = 1.414201183431952662721893491124260
577 / 408 = 1.414215686274509803921568627450980
1393 / 985 = 1.414213197969543147208121827411168
3363 / 2378 = 1.414213624894869638351555929352397
 
Task 4, Pell primes:
2
5
29
5741
33461
44560482149
1746860020068409
68480406462161287469
13558774610046711780701
4125636888562548868221559797461449
 
Task 5, Pell indices of Pell primes:2 3 5 11 13 29 41 53 59 89
 
Task 6, Newman-Shank-Williams numbers: [1, 7, 41, 239, 1393, 8119, 47321, 275807, 1607521, 9369319]
 
Task , Near isoseles 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)
</pre>
 
=={{header|Julia}}==
Line 1,202 ⟶ 1,409:
(4684659, 4684660, 6625109)
(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 := 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 style="height:40ex;overflow:scroll;">
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:
[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]
</pre>
 
Line 1,448 ⟶ 1,795:
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python3">
# pell_numbers.py by Xing216
def is_prime(n):
if n == 1:
return False
i = 2
while i*i <= n:
if n % i == 0:
return False
i += 1
return True
def pell(p0: int,p1: int,its: int):
nums = [p0,p1]
primes = {}
idx = 2
while len(nums) != its:
p = 2*nums[-1]+nums[-2]
if is_prime(p):
primes[idx] = p
nums.append(p)
idx += 1
return nums, primes
def nsw(its: int,pell_nos: list):
nums = []
for i in range(its):
nums.append(pell_nos[2*i] + pell_nos[2*i+1])
return nums
def pt(its: int, pell_nos: list):
nums = []
for i in range(1,its+1):
hypot = pell_nos[2*i+1]
shorter_leg = sum(pell_nos[:2*i+1])
longer_leg = shorter_leg + 1
nums.append((shorter_leg,longer_leg,hypot))
return nums
pell_nos, pell_primes = pell(0,1,50)
pell_lucas_nos, pl_primes = pell(2,2,10)
nsw_nos = nsw(10, pell_nos)
pythag_triples = pt(10, pell_nos)
sqrt2_approx = {}
for idx, pell_no in enumerate(pell_nos):
numer = pell_nos[idx-1] + pell_no
if pell_no != 0:
sqrt2_approx[f"{numer}/{pell_no}"] = numer/pell_no
print(f"The first 10 Pell Numbers:\n {' '.join([str(_) for _ in pell_nos[:10]])}")
print(f"The first 10 Pell-Lucas Numbers:\n {' '.join([str(_) for _ in pell_lucas_nos])}")
print(f"The first 10 rational and decimal approximations of sqrt(2) ({(2**0.5):.10f}):")
print(" rational | decimal")
for rational in list(sqrt2_approx.keys())[:10]:
print(f"{rational:>10} ≈ {sqrt2_approx[rational]:.10f}")
print("The first 7 Pell Primes:")
print(" index | Pell Prime")
for idx, prime in pell_primes.items():
print(f"{idx:>6} | {prime}")
print(f"The first 10 Newman-Shank-Williams numbers:\n {' '.join([str(_) for _ in nsw_nos])}")
print(f"The first 10 near isosceles right triangles:")
for i in pythag_triples:
print(f" {i}")
</syntaxhighlight>
{{out}}
<pre style="height: 15em;">
The first 10 Pell Numbers:
0 1 2 5 12 29 70 169 408 985
The first 10 Pell-Lucas Numbers:
2 2 6 14 34 82 198 478 1154 2786
The first 10 rational and decimal approximations of sqrt(2) (1.4142135624):
rational | decimal
1/1 ≈ 1.0000000000
3/2 ≈ 1.5000000000
7/5 ≈ 1.4000000000
17/12 ≈ 1.4166666667
41/29 ≈ 1.4137931034
99/70 ≈ 1.4142857143
239/169 ≈ 1.4142011834
577/408 ≈ 1.4142156863
1393/985 ≈ 1.4142131980
3363/2378 ≈ 1.4142136249
The first 7 Pell Primes:
index | Pell Prime
2 | 2
3 | 5
5 | 29
11 | 5741
13 | 33461
29 | 44560482149
41 | 1746860020068409
The first 10 Newman-Shank-Williams numbers:
1 7 41 239 1393 8119 47321 275807 1607521 9369319
The first 10 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)
</pre>
=={{header|Quackery}}==
 
Line 2,006 ⟶ 2,454:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt, BigRat
import "./math" for Int
import "./fmt" for Fmt
3,044

edits