From 0a9b85926bf1bc18770397f058fdb6e4aebebabd Mon Sep 17 00:00:00 2001 From: Polgár Márton Date: Mon, 4 Jul 2022 10:56:38 +0200 Subject: Solution for first task --- challenge-172/2colours/raku/ch-1.raku | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 challenge-172/2colours/raku/ch-1.raku diff --git a/challenge-172/2colours/raku/ch-1.raku b/challenge-172/2colours/raku/ch-1.raku new file mode 100755 index 0000000000..0ee9b236ff --- /dev/null +++ b/challenge-172/2colours/raku/ch-1.raku @@ -0,0 +1,32 @@ +#!/usr/bin/env raku + +constant @primes = (1 .. *).grep: &is-prime; + +multi prime-partition($sum, $count) { + gather samewith $sum, $count, 1, () +} + +#bound check (could be more accurate for sure) +sub unfinishable($sum, $count, $chosen-prime) { $sum < $count * $chosen-prime } + +#terminating when done +multi prime-partition($sum, 0, $, @rest) { + take @rest if $sum == 0; +} + +# +multi prime-partition($sum, $count, $lower-bound, @rest) { + my &finishable = { !unfinishable($sum, $count, $_) }; + my @prime-range = lazy @primes.toggle: :off, * > $lower-bound; + @prime-range .= toggle: &finishable; + samewith $sum - $_, $count - 1, $_, (|@rest, $_) for @prime-range; +} + +sub MAIN( + Int $m, + Int $n +) { + say "Input: \$m = $m, \$n = $n"; + my $output = prime-partition($m, $n).map(*.join: ', ').join: ' or '; + say "Output: $output"; +} \ No newline at end of file -- cgit