From 6afc92bb1d9f1a4444bda22f20bbb173ab2a02aa Mon Sep 17 00:00:00 2001 From: Randy Lauen Date: Tue, 27 Aug 2019 23:01:08 -0500 Subject: some solutions --- challenge-023/randy-lauen/perl5/ch-1.pl | 30 +++++++++++++++++++ challenge-023/randy-lauen/perl6/ch-1.p6 | 27 +++++++++++++++++ challenge-023/randy-lauen/perl6/ch-2.p6 | 53 +++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 challenge-023/randy-lauen/perl5/ch-1.pl create mode 100644 challenge-023/randy-lauen/perl6/ch-1.p6 create mode 100644 challenge-023/randy-lauen/perl6/ch-2.p6 diff --git a/challenge-023/randy-lauen/perl5/ch-1.pl b/challenge-023/randy-lauen/perl5/ch-1.pl new file mode 100644 index 0000000000..2b8533c330 --- /dev/null +++ b/challenge-023/randy-lauen/perl5/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +=head1 SYNOPSIS + +Task: +Create a script that prints nth order forward difference series. You should be a +able to pass the list of numbers and order number as command line parameters. + +Usage: + $ perl ch-1.pl --order=1 5 9 2 8 1 6 + +=cut + +use strict; +use warnings; +use feature 'say'; + +use Getopt::Long; + +my $order; +GetOptions( 'order=i' => \$order ) or die; + +my @numbers = @ARGV; + +foreach my $order ( 1 .. $order ) { + @numbers = map { $numbers[ $_ ] - $numbers[ $_ - 1 ] } 1 .. $#numbers; + say "$order: " . join(', ', @numbers); + last if @numbers == 1; +} + diff --git a/challenge-023/randy-lauen/perl6/ch-1.p6 b/challenge-023/randy-lauen/perl6/ch-1.p6 new file mode 100644 index 0000000000..a239b28586 --- /dev/null +++ b/challenge-023/randy-lauen/perl6/ch-1.p6 @@ -0,0 +1,27 @@ +#!/usr/bin/env perl6 + +=begin SYNOPSIS + +Task: +Create a script that prints nth order forward difference series. You should be a +able to pass the list of numbers and order number as command line parameters. + +Usage: + $ perl6 ch-1.p6 --order=1 5 9 2 8 1 6 + +Notes: +I don't really understand all the intricacies of the << hyper operator and what +the different pointy directions mean, especially when the lists are different +sizes. But, after fiddling with it in the perl6 REPL, I managed to find the +behavior I was looking for. + +=end SYNOPSIS + +sub MAIN( Int :$order!, *@numbers where *.elems > 0 ) { + for 1 .. $order -> $i { + @numbers = @numbers.tail(*-1) >>->> @numbers; + say "$i: @numbers.join(', ')"; + last if @numbers.elems == 1; + } +} + diff --git a/challenge-023/randy-lauen/perl6/ch-2.p6 b/challenge-023/randy-lauen/perl6/ch-2.p6 new file mode 100644 index 0000000000..3669bfafa6 --- /dev/null +++ b/challenge-023/randy-lauen/perl6/ch-2.p6 @@ -0,0 +1,53 @@ +#!/usr/bin/env perl6 + +=begin SYNOPSIS + +Task: +Create a script that prints Prime Decomposition of a given number. The prime decomposition of a number is +defined as a list of prime numbers which when all multiplied together, are equal to that number. For example, +the Prime decomposition of 228 is 2,2,3,19 as 228 = 2 * 2 * 3 * 19. + +Usage: + $ perl6 ch-2.p6 228 + $ perl6 ch-2.p6 --test + +Notes: +I used the algorithm described here: https://www.geeksforgeeks.org/print-all-prime-factors-of-a-given-number/ + +=end SYNOPSIS + +sub my-prime-factors( $n is copy ) { + my @factors; + while $n %% 2 { + @factors.push: 2; + $n /= 2; + } + for 3, 5 ... sqrt($n) -> $i { + while $n %% $i { + @factors.push: $i; + $n /= $i; + } + } + @factors.push: $n if $n > 2; + + return @factors; +} + + +multi MAIN( Int $input where * > 0 ) { + my @factors = my-prime-factors( $input ); + say @factors.elems + ?? @factors.join(', ') + !! "No prime factors for: $input"; + ; +} + + +multi MAIN( Bool :$test! ) { + use Test; + for 1 .. 1000 -> $n { + say "$n: " ~ my-prime-factors($n).join(', '); + } +} + + -- cgit