diff options
| author | Simon Proctor <simon.proctor@zpg.co.uk> | 2019-08-27 09:54:22 +0100 |
|---|---|---|
| committer | Simon Proctor <simon.proctor@zpg.co.uk> | 2019-08-27 09:54:22 +0100 |
| commit | ecdb90ff38ebb7daea6d414957b6efe0937e57fb (patch) | |
| tree | 627ecb5bab99e7028b46687ebad498a76b0dbed4 | |
| parent | 338f556dd07c935fab06e3117ddd7f9674ffade0 (diff) | |
| download | perlweeklychallenge-club-ecdb90ff38ebb7daea6d414957b6efe0937e57fb.tar.gz perlweeklychallenge-club-ecdb90ff38ebb7daea6d414957b6efe0937e57fb.tar.bz2 perlweeklychallenge-club-ecdb90ff38ebb7daea6d414957b6efe0937e57fb.zip | |
Challenge 23 solutions post bank holiday
| -rw-r--r-- | challenge-023/simon-proctor/perl6/ch-1.p6 | 20 | ||||
| -rw-r--r-- | challenge-023/simon-proctor/perl6/ch-2.p6 | 34 |
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-023/simon-proctor/perl6/ch-1.p6 b/challenge-023/simon-proctor/perl6/ch-1.p6 new file mode 100644 index 0000000000..b1b6e636a0 --- /dev/null +++ b/challenge-023/simon-proctor/perl6/ch-1.p6 @@ -0,0 +1,20 @@ +#!/usr/bin.env perl6 + +use v6; + +#| Display help +multi sub MAIN( Bool :h(:$help) where so * ) { + say $*USAGE; +} + +#| Calculate the Nth forward difference series of the given values +multi sub MAIN( + UInt $n, #= Order to calculate + *@vals where @vals.all ~~ Int #= List of Integers to calculate from +) { + die "N must between 1 and {@vals.elems-1}" unless 0 < $n <= @vals.elems-1; + for ^$n { + @vals = @vals.rotor(2=>-1).map( { $_[1] - $_[0] }) + } + say @vals.join(","); +} diff --git a/challenge-023/simon-proctor/perl6/ch-2.p6 b/challenge-023/simon-proctor/perl6/ch-2.p6 new file mode 100644 index 0000000000..acdc46fcb7 --- /dev/null +++ b/challenge-023/simon-proctor/perl6/ch-2.p6 @@ -0,0 +1,34 @@ +#!/usr/bin/env perl6 + +use v6; + +#| Display Help +multi sub MAIN( Bool :h(:$help) where so * ) { + say $*USAGE; +} + +multi sub MAIN( 1 ) is hidden-from-USAGE { 1 } + +#| Print the list of prime multiples of the given value +multi sub MAIN ( + UInt $value #= Value to calulate multiples for +) { + + my $working = $value; + my @out; + while ! $working.is-prime { + my $div = least-prime-divisor( $working ); + @out.push( $div ); + $working = $working div $div; + } + @out.push($working); + say @out.join(","); +} + +multi sub least-prime-divisor( UInt() $n where $n.is-prime ) { + $n; +} + +multi sub least-prime-divisor( UInt() $n ) { + (2..$n).grep( *.is-prime ).first( { $n %% $_ } ); +} |
