Jump to content

Pell numbers: Difference between revisions

3,209 bytes added ,  10 months ago
(Added Algol W)
Line 1,778:
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
require 'openssl'
def prime?(n) = OpenSSL::BN.new(n).prime?
 
pell = Enumerator.new do |y|
pe = [0,1]
loop{y << pe[0]; pe << pe[1]*2 + pe.shift}
end
 
pell_lucas = Enumerator.new do |y|
pe = [2,2]
loop{y << pe[0]; pe << pe[1]*2 + pe.shift}
end
 
n = 20
puts "First #{n} Pell numbers: #{pell.first(n).to_a.inspect}"
puts "\nFirst #{n} Pell-Lucas numbers: #{pell_lucas.first(n).to_a.inspect}"
 
n = 15
sqrt2 = pell.each_cons(2).lazy.map{|p1,p2| Rational(p1+p2, p2)}.take(n).to_a
puts "\nThe first #{n} rational approximations of √2 (#{Math.sqrt(2)}) are:"
sqrt2.each{|n| puts "%-16s ≈ %-18s\n"% [n, n.to_f]}
 
puts "\nThe first #{n} Pell primes with index are:"
primes = pell.with_index.lazy.select{|n, i|prime?(i) && prime?(n)}.first(n)
primes.each {|prime, i| puts "#{i.to_s.ljust(3)} #{prime}"}
 
puts "\nThe first #{n} NSW numbers:"
puts pell.first(2*n).each_slice(2).map(&:sum).join(", ")
 
puts "\nFirst #{n} near isosceles right triangles:"
sum = 1
pell.take(n*2+2).each_slice(2) do |p1,p2|
next if p1 == 0
sum += p1
puts "#{sum}, #{sum+1}, #{p2}"
sum += p2
end
</syntaxhighlight>
{{out}}
<pre>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]
 
The first 15 rational approximations of √2 (1.4142135623730951) are:
1/1 ≈ 1.0
3/2 ≈ 1.5
7/5 ≈ 1.4
17/12 ≈ 1.4166666666666667
41/29 ≈ 1.4137931034482758
99/70 ≈ 1.4142857142857144
239/169 ≈ 1.4142011834319526
577/408 ≈ 1.4142156862745099
1393/985 ≈ 1.4142131979695431
3363/2378 ≈ 1.4142136248948696
8119/5741 ≈ 1.4142135516460548
19601/13860 ≈ 1.4142135642135643
47321/33461 ≈ 1.4142135620573204
114243/80782 ≈ 1.4142135624272734
275807/195025 ≈ 1.4142135623637995
 
The first 15 Pell primes with index are:
2 2
3 5
5 29
11 5741
13 33461
29 44560482149
41 1746860020068409
53 68480406462161287469
59 13558774610046711780701
89 4125636888562548868221559797461449
97 4760981394323203445293052612223893281
101 161733217200188571081311986634082331709
167 2964793555272799671946653940160950323792169332712780937764687561
181 677413820257085084326543915514677342490435733542987756429585398537901
191 4556285254333448771505063529048046595645004014152457191808671945330235841
 
The first 15 NSW numbers:
1, 7, 41, 239, 1393, 8119, 47321, 275807, 1607521, 9369319, 54608393, 318281039, 1855077841, 10812186007, 63018038201
 
First 15 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
</pre>
=={{header|Scala}}==
<syntaxhighlight lang="scala">val pellNumbers: LazyList[BigInt] =
1,149

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.