diff options
| author | Simon Proctor <simon.proctor@zoopla.co.uk> | 2020-06-15 10:00:18 +0100 |
|---|---|---|
| committer | Simon Proctor <simon.proctor@zoopla.co.uk> | 2020-06-15 10:00:18 +0100 |
| commit | 5834ac4aec05ea3206c8a00ac56b90d2f117812f (patch) | |
| tree | 997cc43176a9adb70d0047a16c9bab93544380b4 | |
| parent | 5bba57922fd66d731b0f6e3380823d632a4e3411 (diff) | |
| download | perlweeklychallenge-club-5834ac4aec05ea3206c8a00ac56b90d2f117812f.tar.gz perlweeklychallenge-club-5834ac4aec05ea3206c8a00ac56b90d2f117812f.tar.bz2 perlweeklychallenge-club-5834ac4aec05ea3206c8a00ac56b90d2f117812f.zip | |
Challenge 1 : Two different solutions
| -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}"; + } + } + } +} |
