diff options
Diffstat (limited to 'challenge-023')
| -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 %% $_ } ); +} |
