diff options
| -rw-r--r-- | challenge-023/daniel-mantovani/perl5/ch-1.pl | 52 | ||||
| -rw-r--r-- | challenge-023/daniel-mantovani/perl5/ch-2.pl | 43 |
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-023/daniel-mantovani/perl5/ch-1.pl b/challenge-023/daniel-mantovani/perl5/ch-1.pl new file mode 100644 index 0000000000..65286a11ff --- /dev/null +++ b/challenge-023/daniel-mantovani/perl5/ch-1.pl @@ -0,0 +1,52 @@ +# 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 v5.10; + +# we start by teading the list of numbers and the order + +my $order = pop @ARGV; # last command line parameter is requested order + +# we are going to define a 2 dimmension array to hold original data and +# calculed differences. [0] index for order is input data by definition + +my @differences; + +$differences[0] = [@ARGV]; # just copy input to order 0 diff + +# now we start calculating differences as many times as $order indicates: + +for my $o ( 0 .. $order - 1 ) { + + # we will calculate $o+1 array from $o + # we will need the amount of elements on current array + my $osize = @{ $differences[$o] }; + last if $osize < 2; # cannot calculate more differences + for my $i ( 0 .. $osize - 2 ) { # will do $osize - 1 substractions + $differences[ $o + 1 ][$i] = + $differences[$o][ $i + 1 ] - $differences[$o][$i]; + } +} + +# now we just print requested order +print "$_ " for @{ $differences[$order] }; +say ''; # final "\n" + +# example: you can call this script to get the 2nd order difference below like: +# $> perl ch-1.pl 5 9 2 8 1 6 2 +# +# and get the corresponding result: +# +# -11 13 -13 12 +# diff --git a/challenge-023/daniel-mantovani/perl5/ch-2.pl b/challenge-023/daniel-mantovani/perl5/ch-2.pl new file mode 100644 index 0000000000..bc1a896fab --- /dev/null +++ b/challenge-023/daniel-mantovani/perl5/ch-2.pl @@ -0,0 +1,43 @@ +# 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 v5.10; + +# we will read the number from command line, like: + +my $number = shift; + +# and start trying to divide by a factor, starting with 2 + +my $factor = 2; + +while ( $number > 1 ) { + if ( $number % $factor ) { # divisibility test: not divisible + $factor++; + + # following line is an optimization, exlplained below + $factor = $number unless $factor < sqrt $number; + } + else { # divisible + $number /= $factor; + + # now we just print $factor, and a comma or final enter following + print $number > 1 ? "$factor, " : "$factor\n"; + } +} + +# the reason $factor is always a prime number is because of the order we +# are following (ascending order). If $factor wasn't a prime, all of its prime divisors would +# have divide $number before, so we would'n actually have $factor now. + +# Also note that if we comment line #23 above, this script will take like forever even for +# not that big factor primes. +# One obvious optimization would be to stop checking when $factor reaches square +# root of current $number, and that's what the mentioned line does +# Even without catching the sqrt calculation (i.e. we are recalculating sqrt on almost every +# iteration) factorizing a prime number like 982451653 on my system improves form 53 seconds when +# I comment that line, to .01 seconds when uncommented |
