Pell numbers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (Promote. multiple implementations, no questions) |
(Added Go) |
||
Line 231: | Line 231: | ||
[1235216565974040, 1235216565974041, 1746860020068409]</pre> |
[1235216565974040, 1235216565974041, 1746860020068409]</pre> |
||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
{{libheader|Go-rcu}} |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"math/big" |
|||
"rcu" |
|||
) |
|||
func main() { |
|||
p := make([]int64, 40) |
|||
p[1] = 1 |
|||
for i := 2; i < 40; i++ { |
|||
p[i] = 2*p[i-1] + p[i-2] |
|||
} |
|||
fmt.Println("The first 20 Pell numbers are:") |
|||
fmt.Println(p[0:20]) |
|||
q := make([]int64, 40) |
|||
q[0] = 2 |
|||
q[1] = 2 |
|||
for i := 2; i < 40; i++ { |
|||
q[i] = 2*q[i-1] + q[i-2] |
|||
} |
|||
fmt.Println("\nThe first 20 Pell-Lucas numbers are:") |
|||
fmt.Println(q[0:20]) |
|||
fmt.Println("\nThe first 20 rational approximations of √2 (1.4142135623730951) are:") |
|||
for i := 1; i <= 20; i++ { |
|||
r := big.NewRat(q[i]/2, p[i]) |
|||
fmt.Printf("%-17s ≈ %-18s\n", r, r.FloatString(16)) |
|||
} |
|||
fmt.Println("\nThe first 15 Pell primes are:") |
|||
p0 := big.NewInt(0) |
|||
p1 := big.NewInt(1) |
|||
p2 := big.NewInt(0) |
|||
two := big.NewInt(2) |
|||
indices := make([]int, 15) |
|||
for index, count := 2, 0; count < 15; index++ { |
|||
p2.Mul(p1, two) |
|||
p2.Add(p2, p0) |
|||
if rcu.IsPrime(index) && p2.ProbablyPrime(15) { |
|||
fmt.Println(p2) |
|||
indices[count] = index |
|||
count++ |
|||
} |
|||
p0.Set(p1) |
|||
p1.Set(p2) |
|||
} |
|||
fmt.Println("\nIndices of the first 15 Pell primes are:") |
|||
fmt.Println(indices) |
|||
fmt.Println("\nFirst 20 Newman-Shank_Williams numbers:") |
|||
nsw := make([]int64, 20) |
|||
for n := 0; n < 20; n++ { |
|||
nsw[n] = p[2*n] + p[2*n+1] |
|||
} |
|||
fmt.Println(nsw) |
|||
fmt.Println("\nFirst 20 near isosceles right triangles:") |
|||
u0 := 0 |
|||
u1 := 1 |
|||
sum := 1 |
|||
for i := 2; i < 43; i++ { |
|||
u2 := u1*2 + u0 |
|||
if i%2 == 1 { |
|||
fmt.Printf("(%d, %d, %d)\n", sum, sum+1, u2) |
|||
} |
|||
sum += u2 |
|||
u0 = u1 |
|||
u1 = u2 |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre style="height:40ex;overflow:scroll;"> |
|||
The first 20 Pell numbers are: |
|||
[0 1 2 5 12 29 70 169 408 985 2378 5741 13860 33461 80782 195025 470832 1136689 2744210 6625109] |
|||
The first 20 Pell-Lucas numbers are: |
|||
[2 2 6 14 34 82 198 478 1154 2786 6726 16238 39202 94642 228486 551614 1331714 3215042 7761798 18738638] |
|||
The first 20 rational approximations of √2 (1.4142135623730951) are: |
|||
1/1 ≈ 1.0000000000000000 |
|||
3/2 ≈ 1.5000000000000000 |
|||
7/5 ≈ 1.4000000000000000 |
|||
17/12 ≈ 1.4166666666666667 |
|||
41/29 ≈ 1.4137931034482759 |
|||
99/70 ≈ 1.4142857142857143 |
|||
239/169 ≈ 1.4142011834319527 |
|||
577/408 ≈ 1.4142156862745098 |
|||
1393/985 ≈ 1.4142131979695431 |
|||
3363/2378 ≈ 1.4142136248948696 |
|||
8119/5741 ≈ 1.4142135516460547 |
|||
19601/13860 ≈ 1.4142135642135642 |
|||
47321/33461 ≈ 1.4142135620573205 |
|||
114243/80782 ≈ 1.4142135624272734 |
|||
275807/195025 ≈ 1.4142135623637995 |
|||
665857/470832 ≈ 1.4142135623746899 |
|||
1607521/1136689 ≈ 1.4142135623728214 |
|||
3880899/2744210 ≈ 1.4142135623731420 |
|||
9369319/6625109 ≈ 1.4142135623730870 |
|||
22619537/15994428 ≈ 1.4142135623730964 |
|||
The first 15 Pell primes are: |
|||
2 |
|||
5 |
|||
29 |
|||
5741 |
|||
33461 |
|||
44560482149 |
|||
1746860020068409 |
|||
68480406462161287469 |
|||
13558774610046711780701 |
|||
4125636888562548868221559797461449 |
|||
4760981394323203445293052612223893281 |
|||
161733217200188571081311986634082331709 |
|||
2964793555272799671946653940160950323792169332712780937764687561 |
|||
677413820257085084326543915514677342490435733542987756429585398537901 |
|||
4556285254333448771505063529048046595645004014152457191808671945330235841 |
|||
Indices of the first 15 Pell primes are: |
|||
[2 3 5 11 13 29 41 53 59 89 97 101 167 181 191] |
|||
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|J}}== |
=={{header|J}}== |