diff options
| author | Mark A <andemark@a-iot1t.uch.ad.pvt> | 2021-08-10 16:20:49 -0600 |
|---|---|---|
| committer | Mark A <andemark@a-iot1t.uch.ad.pvt> | 2021-08-10 16:20:49 -0600 |
| commit | 2d9ee97d6bb8ea907dded432b0bce9193130e1cc (patch) | |
| tree | d844f72472de2379bb863815e0b2b545f0a95390 | |
| parent | f53f1dbe91a9b7bae72ae34af9f9438bbcd770a1 (diff) | |
| download | perlweeklychallenge-club-2d9ee97d6bb8ea907dded432b0bce9193130e1cc.tar.gz perlweeklychallenge-club-2d9ee97d6bb8ea907dded432b0bce9193130e1cc.tar.bz2 perlweeklychallenge-club-2d9ee97d6bb8ea907dded432b0bce9193130e1cc.zip | |
ch-1.raku
| -rw-r--r-- | challenge-125/mark-anderson/raku/ch-1.raku | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/challenge-125/mark-anderson/raku/ch-1.raku b/challenge-125/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..34ef0dd0dd --- /dev/null +++ b/challenge-125/mark-anderson/raku/ch-1.raku @@ -0,0 +1,58 @@ +#!/usr/bin/env raku + +# +# https://en.wikipedia.org/wiki/Formulas_for_generating_Pythagorean_triples#Dickson's_method +# + +use Prime::Factor; + +multi sub MAIN($N) +{ + .say for py-tris($N); +} + +multi sub MAIN +{ + use Test; + plan 9; + + is py-tris(1), -1; + is py-tris(2), -1; + is-deeply py-tris(3), ((3, 4, 5),); + is-deeply py-tris(5), ((3, 4, 5), (5, 12, 13)); + is-deeply py-tris(6), ((6, 8, 10),); + is-deeply py-tris(13), ((5, 12, 13), (13, 84, 85)); + is-deeply py-tris(15), ((8, 15, 17), (9, 12, 15), (15, 20, 25), (15, 36, 39), (15, 112, 113)); + is-deeply py-tris(33), ((33, 44, 55), (33, 56, 65), (33, 180, 183), (33, 544, 545)); + is-deeply py-tris(40), ((9, 40, 41), (24, 32, 40), (30, 40, 50), (40, 42, 58), (40, 75, 85), (40, 96, 104), (40, 198, 202), (40, 399, 401)); +} + +multi py-tris($N where * == 1|2) { -1 } + +multi py-tris($N where * == 3|4) { (3,4,5), } + +multi py-tris($N where * > 0) +{ + gather + { + SEQ: for 2, 4 ... * -> $r + { + for factor-pairs(($r**2/2).Int) -> $p + { + my $x = $r + $p[0]; + my $y = $r + $p[1]; + my $z = $r + $p[0] + $p[1]; + + take ($x, $y, $z) if any($x, $y, $z) == $N; + last SEQ if $x == $N and $z - $y == 1|2; + } + } + } +} + +sub factor-pairs($n) +{ + my @divs = proper-divisors($n); + @divs = @divs.grep(* < sqrt($n)); + @divs.map({ $_, $n div $_ }); +} |
