diff options
| -rw-r--r-- | challenge-065/simon-proctor/raku/ch-1.p6 | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-065/simon-proctor/raku/ch-1.p6 b/challenge-065/simon-proctor/raku/ch-1.p6 new file mode 100644 index 0000000000..9843b1459e --- /dev/null +++ b/challenge-065/simon-proctor/raku/ch-1.p6 @@ -0,0 +1,36 @@ +#!/usr/bin/env raku + +use v6; + +multi sub MAIN ( + UInt $n, #= Number of digits + UInt $s, #= Target number + Bool :b(:$brute) where *.so #= Use Brute Force Technique +) { + .say for ( (1 x $n).Int..(9 x $n).Int ).hyper.grep( { ([+] $_.comb) ~~ $s } ); +} + +multi sub MAIN ( + UInt $n, #= Number of digits + UInt $s, #= Target number + Bool :b(:$brute) = False #= Use Brute Force Technique +) { + .say for calculate-sums( $n, $s, 1 ).grep( { defined $_ } ); +} + +multi sub calculate-sums( UInt $n, UInt $s where { $n * 9 < $s }, $ ) { gather take Nil } + +multi sub calculate-sums( UInt $n, 0, $ ) { gather take 0 x $n } + +multi sub calculate-sums( UInt $n, UInt $s, $start = 0 ) { + my $end = $s > 9 ?? 9 !! $s; + + gather { + for $start..$end -> $val { + for calculate-sums( $n-1, $s-$val, 0 ) -> $sum { + next unless defined $sum; + take "{$val}{$sum}"; + } + } + } +} |
