From 2a1326057fbb4e0435f05f1b7b1f1bad4465a3dc Mon Sep 17 00:00:00 2001 From: Doomtrain14 Date: Mon, 26 Aug 2019 22:33:26 +0800 Subject: Added solution for ch23 --- challenge-023/yet-ebreo/perl5/ch-1.pl | 23 +++++++++++++++++++ challenge-023/yet-ebreo/perl5/ch-2.pl | 42 +++++++++++++++++++++++++++++++++++ challenge-023/yet-ebreo/perl6/ch-1.p6 | 19 ++++++++++++++++ challenge-023/yet-ebreo/perl6/ch-2.p6 | 31 ++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 challenge-023/yet-ebreo/perl5/ch-1.pl create mode 100644 challenge-023/yet-ebreo/perl5/ch-2.pl create mode 100644 challenge-023/yet-ebreo/perl6/ch-1.p6 create mode 100644 challenge-023/yet-ebreo/perl6/ch-2.p6 diff --git a/challenge-023/yet-ebreo/perl5/ch-1.pl b/challenge-023/yet-ebreo/perl5/ch-1.pl new file mode 100644 index 0000000000..36198da740 --- /dev/null +++ b/challenge-023/yet-ebreo/perl5/ch-1.pl @@ -0,0 +1,23 @@ +# 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. +# Let me show you with an example. +# Suppose we have list (X) of numbers: 5, 9, 2, 8, 1, 6 and we would like to +# create 1st order forward difference series (Y). So using the formula Y(i) = X(i+1) - X(i), +# we get the following numbers: (9-5), (2-9), (8-2), (1-8), (6-1). In short, +# the final series would be: 4, -7, 6, -7, 5. If you noticed, it has one less number +# than the original series. Similary you can carry on 2nd order forward difference series like: +# (-7-4), (6+7), (-7-6), (5+7) => -11, 13, -13, 12. + +use strict; +use warnings; +use 5.010; + +my ($n, @list) = @ARGV; + +die "Usage: ch-1.pl \n\tn must be less than the number of elements in list\n\tlist values are space separated\n" if !@ARGV; +die "n($n) must be less than the number(".@list.") of elements in list\n" if $n>= @list; + + +(@list = map $list[$_]-$list[$_-1],1..$#list) for 1..$n; + +say "@list"; \ No newline at end of file diff --git a/challenge-023/yet-ebreo/perl5/ch-2.pl b/challenge-023/yet-ebreo/perl5/ch-2.pl new file mode 100644 index 0000000000..8af3986a73 --- /dev/null +++ b/challenge-023/yet-ebreo/perl5/ch-2.pl @@ -0,0 +1,42 @@ +# 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. +use strict; +use warnings; +use 5.010; + +my @r; +my $n = $ARGV[0]; +my $m = $n; +die "Usage: ch-2.pl \n\tn is a postive number\n" if !@ARGV; +die "n must be a postive number\n" if $n<1; + +#Backticks solution using factor, works both on windows and linux :D +say "Using backticks and factor system command:"; +say `factor $n`=~s/.+: //r; + +#Using ntheory module +say "Using module nthoery"; +use ntheory 'factor'; +@r = factor $m; +say "@r\n"; + +#But I feel like I should do a non-backtick/module solution so here we go: +#It's slow(and inaccurate) on very large numbers +say "Using non-backticks solution (trial division/modulo):"; +@r = (); +while ($n % 2<1) { + push @r, 2; + $n /= 2; +} +my $f = 3; +while ($f*$f<=$n) { + while ($n % $f<1) { + push @r, $f; + $n /= $f; + } + $f+=2; +} +$n>1 && push @r, $n; +say "@r\n"; diff --git a/challenge-023/yet-ebreo/perl6/ch-1.p6 b/challenge-023/yet-ebreo/perl6/ch-1.p6 new file mode 100644 index 0000000000..bc69ef39d1 --- /dev/null +++ b/challenge-023/yet-ebreo/perl6/ch-1.p6 @@ -0,0 +1,19 @@ +# 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. +# Let me show you with an example. +# Suppose we have list (X) of numbers: 5, 9, 2, 8, 1, 6 and we would like to +# create 1st order forward difference series (Y). So using the formula Y(i) = X(i+1) - X(i), +# we get the following numbers: (9-5), (2-9), (8-2), (1-8), (6-1). In short, +# the final series would be: 4, -7, 6, -7, 5. If you noticed, it has one less number +# than the original series. Similary you can carry on 2nd order forward difference series like: +# (-7-4), (6+7), (-7-6), (5+7) => -11, 13, -13, 12. + +my ($n, @list) = @*ARGS; + +die "Usage: ch-1.pl \n\tn\tmust be less than the number of elements in list\n\tlist\tvalues are space separated\n" if !@*ARGS; +die "n($n) must be less than the number("~@list.end+1~") of elements in list\n" if $n> @list.end; + + +(@list = map {@list[$_]-@list[$_-1]},1..@list.end) for 1..$n; + +say "@list[]"; diff --git a/challenge-023/yet-ebreo/perl6/ch-2.p6 b/challenge-023/yet-ebreo/perl6/ch-2.p6 new file mode 100644 index 0000000000..f3996115cc --- /dev/null +++ b/challenge-023/yet-ebreo/perl6/ch-2.p6 @@ -0,0 +1,31 @@ +# 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. + + +my $n = @*ARGS[0]; +my $m = $n; +die "Usage: ch-2.pl \n\tn is a postive number\n" if !@*ARGS; +die "n must be a postive number\n" if $n < 1; + +#Backticks solution using factor, works both on windows and linux :D +say "Using backticks and factor system command:"; +say qqx{factor $n}.subst(/.+\:\s/, ''); + +say "Using non-backticks solution (trial division/modulo):"; +my @r; +while ($n % 2 < 1) { + push @r, 2; + $n /= 2; +} +my $f = 3; +while ($f*$f <= $n) { + while ($n % $f < 1) { + push @r, $f; + $n /= $f; + } + $f+=2; +} +$n > 1 && push @r, $n; +say "@r[]\n"; -- cgit