Pell numbers: Difference between revisions

(Added Go)
Line 797:
(211929657785303, 211929657785304, 299713796309065)
(1235216565974040, 1235216565974041, 1746860020068409)</pre>
 
=={{header|Scala}}==
<lang scala>val pellNumbers: LazyList[BigInt] =
BigInt("0") #:: BigInt("1") #::
(pellNumbers zip pellNumbers.tail).
map{ case (a,b) => 2*b + a }
 
val pellLucasNumbers: LazyList[BigInt] =
BigInt("2") #:: BigInt("2") #::
(pellLucasNumbers zip pellLucasNumbers.tail).
map{ case (a,b) => 2*b + a }
 
val pellPrimes: LazyList[BigInt] =
pellNumbers.tail.tail.
filter{ case p => p.isProbablePrime(16) }
 
val pellIndexOfPrimes: LazyList[BigInt] =
pellNumbers.tail.tail.
filter{ case p => p.isProbablePrime(16) }.
map{ case p => pellNumbers.indexOf(p) }
 
val pellNSWnumbers: LazyList[BigInt] =
(pellNumbers.zipWithIndex.collect{ case (p,i) if i%2 == 0 => p}
zip
pellNumbers.zipWithIndex.collect{ case (p,i) if i%2 != 0 => p}).
map{ case (a,b) => a + b }
 
val pellSqrt2Numerator: LazyList[BigInt] =
BigInt(1) #:: BigInt(3) #::
(pellSqrt2Numerator zip pellSqrt2Numerator.tail).
map{ case (a,b) => 2*b + a }
 
val pellSqrt2: LazyList[BigDecimal] =
(pellSqrt2Numerator zip pellNumbers.tail).
map{ case (n,d) => BigDecimal(n)/BigDecimal(d) }
 
val pellSqrt2asString: LazyList[String] =
(pellSqrt2Numerator zip pellNumbers.tail).
map{ case (n,d) => s"$n/$d" }
 
val pellHypotenuse: LazyList[BigInt] =
pellNumbers.tail.tail.zipWithIndex.collect{ case (p,i) if i%2 != 0 => p }
 
val pellShortLeg: LazyList[BigInt] =
LazyList.from(3,2).map{ case s => pellNumbers.take(s).sum }
 
val pellTriple: LazyList[(BigInt,BigInt,BigInt)] =
(pellHypotenuse zip pellShortLeg).
map{ case (h,s) => (s,s+1,h)}
 
// Output
{ println("7 Tasks")
println("-------")
println(pellNumbers.take(10).mkString("1. Pell Numbers: ", ",", "\n"))
println(pellLucasNumbers.take(10).mkString("2. Pell-Lucas Numbers: ", ",", "\n"))
println((pellSqrt2asString zip pellSqrt2).take(10).
map { case (f, d) => s"$f = $d" }.
mkString("3. Square-root of 2 Approximations: \n\n",
"\n", "\n"))
println(pellPrimes.take(10).mkString("4. Pell Primes: \n\n", "\n", "\n"))
println(pellIndexOfPrimes.take(10).mkString("5. Pell Index of Primes: ", ",", "\n"))
println(pellNSWnumbers.take(10).mkString("6. Newman-Shank-Williams Numbers: \n\n", "\n", "\n"))
println(pellTriple.take(10).mkString("7. Near Right-triangle Triples: \n\n", "\n", "\n"))
}
</lang>
{{out}}
<pre style="height:48ex;overflow:scroll;>7 Tasks
-------
1. Pell Numbers: 0,1,2,5,12,29,70,169,408,985
 
2. Pell-Lucas Numbers: 2,2,6,14,34,82,198,478,1154,2786
 
3. Square-root of 2 Approximations:
 
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
 
4. Pell Primes:
 
2
5
29
5741
33461
44560482149
1746860020068409
68480406462161287469
13558774610046711780701
4125636888562548868221559797461449
 
5. Pell Index of Primes: 2,3,5,11,13,29,41,53,59,89
 
6. Newman-Shank-Williams Numbers:
 
1
7
41
239
1393
8119
47321
275807
1607521
9369319
 
7. Near Right-triangle Triples:
 
(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|Wren}}==
105

edits