RosettaCodeData/Task/Pell-numbers/Raku/pell-numbers.raku
2023-07-01 13:44:08 -04:00

20 lines
869 B
Raku
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

my $pell = cache lazy 0, 1, * + * × 2 *;
my $pell-lucas = lazy 2, 2, * + * × 2 *;
my $upto = 20;
say "First $upto Pell numbers:\n" ~ $pell[^$upto];
say "\nFirst $upto Pell-Lucas numbers:\n" ~ $pell-lucas[^$upto];
say "\nFirst $upto rational approximations of 2 ({sqrt(2)}):\n" ~
(1..$upto).map({ sprintf "%d/%d - %1.16f", $pell[$_-1] + $pell[$_], $pell[$_], ($pell[$_-1]+$pell[$_])/$pell[$_] }).join: "\n";
say "\nFirst $upto Pell primes:\n" ~ $pell.grep(&is-prime)[^$upto].join: "\n";
say "\nIndices of first $upto Pell primes:\n" ~ (^).grep({$pell[$_].is-prime})[^$upto];
say "\nFirst $upto Newman-Shank-Williams numbers:\n" ~ (^$upto).map({ $pell[2 × $_, 2 × $_+1].sum });
say "\nFirst $upto near isosceles right triangles:";
map -> \p { printf "(%d, %d, %d)\n", |($_, $_+1 given $pell[^(2 × p + 1)].sum), $pell[2 × p + 1] }, 1..$upto;