diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-08-30 21:15:26 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-08-30 21:15:26 +0100 |
| commit | d1557538fe8ce48c40ed1d527e299cc285b4921f (patch) | |
| tree | de3d8476b2f2133aa1366f4bd7cfaa4574b7c348 | |
| parent | 887c31c0ef2ef5e5dcf151d95c6304ceafa883df (diff) | |
| parent | 9d7c4adad7c8be15fa19998c7507e20a51e84203 (diff) | |
| download | perlweeklychallenge-club-d1557538fe8ce48c40ed1d527e299cc285b4921f.tar.gz perlweeklychallenge-club-d1557538fe8ce48c40ed1d527e299cc285b4921f.tar.bz2 perlweeklychallenge-club-d1557538fe8ce48c40ed1d527e299cc285b4921f.zip | |
Merge pull request #574 from waltman/branch-for-challenge-023
Branch for challenge 023
| -rw-r--r-- | challenge-023/walt-mankowski/perl5/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-023/walt-mankowski/perl5/ch-2.pl | 43 |
2 files changed, 89 insertions, 0 deletions
diff --git a/challenge-023/walt-mankowski/perl5/ch-1.pl b/challenge-023/walt-mankowski/perl5/ch-1.pl new file mode 100644 index 0000000000..6889defbf6 --- /dev/null +++ b/challenge-023/walt-mankowski/perl5/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +# 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. +# +# The order is passed as the first parameter to the script, followed +# by the list of items. For example, to compute the first order forward +# difference series of the list above, we'd run +# +# perl ch-1.pl 1 5 9 2 8 1 6 +# 4 -7 6 -7 5 +# +# To compute the second order we'd run +# +# perl ch-1.pl 2 5 9 2 8 1 6 +# -11 13 -13 12 + +use strict; +use warnings; +use feature qw(:5.30); +use experimental qw(signatures); + +my ($n, @X) = @ARGV; + +my @Y = nth_order_forward_diff($n, @X); +say "@Y"; + +sub nth_order_forward_diff($n, @X) { + for (1..$n) { + my @Y; + for my $i (0..$#X - 1) { + $Y[$i] = $X[$i+1] - $X[$i]; + } + @X = @Y; + } + return @X; +} diff --git a/challenge-023/walt-mankowski/perl5/ch-2.pl b/challenge-023/walt-mankowski/perl5/ch-2.pl new file mode 100644 index 0000000000..ee8c3d26a0 --- /dev/null +++ b/challenge-023/walt-mankowski/perl5/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# 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. +# +# The code works by first finding the primes up to n using the Sieve +# of Eratosthenes, then seeing how many times each of them divide n +# evenly. + +use strict; +use warnings; +use feature qw(:5.30); +use experimental qw(signatures); + +my $N = $ARGV[0]; +my $n = $N; +my @p = primes_upto($n); +my @factors; +for my $p (@p) { + while ($n % $p == 0) { + push @factors, $p; + $n /= $p; + } +} +printf "%d = %s\n", $N, join "*", @factors; + +# compute primes up to $n using the Sieve of Eratosthenes +sub primes_upto($n) { + my @is_prime = map {1} (0..$n); + $is_prime[0] = $is_prime[1] = 0; + + for my $i (2..$n) { + if ($is_prime[$i]) { + for (my $j = $i * 2; $j <= $n; $j += $i) { + $is_prime[$j] = 0; + } + } + } + return grep {$is_prime[$_]} 2..$n; +} |
