-
Notifications
You must be signed in to change notification settings - Fork 319
/
ch-1.raku
33 lines (28 loc) · 932 Bytes
/
ch-1.raku
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
}