From bc5ebe26f5e8921e985eea19d96ac717942adae3 Mon Sep 17 00:00:00 2001 From: Jan Krňávek Date: Sun, 15 Aug 2021 15:43:47 +0200 Subject: solution week 125-1 --- challenge-125/wambash/raku/ch-1.raku | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 challenge-125/wambash/raku/ch-1.raku diff --git a/challenge-125/wambash/raku/ch-1.raku b/challenge-125/wambash/raku/ch-1.raku new file mode 100644 index 0000000000..5abb5ade24 --- /dev/null +++ b/challenge-125/wambash/raku/ch-1.raku @@ -0,0 +1,33 @@ +#!/usr/bin/env raku + +constant @next-candidates := + (( 1, 2, 2),(-2,-1,-2),(2,2,3)), + (( 1, 2, 2),( 2, 1, 2),(2,2,3)), + ((-1,-2,-2),( 2, 1, 2),(2,2,3)), +; + +sub next-level (@a,:$n) { + @a + andthen @next-candidates.map: $_ «*« * + andthen .map: { [Z+] $_ }\ + andthen .map: *.cache + andthen |.grep: { .any ≤ $n } +} + +sub pythagorean-triples ($n) { # only primitive + ((3,4,5),), { .map: &next-level.assuming(:$n) } ... :!elems + andthen .map: |*.grep: $n == *.any # by `$n %% *.any` you could have generators for all Pythagorean-triples +} + +multi MAIN (Bool :test($)!) { + use Test; + is pythagorean-triples( 5), ((3,4,5),(5,12,13)); + is pythagorean-triples(13), ((5,12,13),(13,84,85)); + is pythagorean-triples( 1), (); + is pythagorean-triples(65), ((33,56,65),(65,72,97),(63,16,65),(65,2112,2113)); + done-testing; +} + +multi MAIN ($n=420) { + say pythagorean-triples($n) || -1 +} -- cgit