aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-08-31 17:27:59 +0100
committerGitHub <noreply@github.com>2019-08-31 17:27:59 +0100
commit3753f54f4fc4628cf6292c5f79f76654b64e46c8 (patch)
treec7a4832ff09e8a0923986cb7d5aacd0e35ab6cf9
parentc5e7fa7dae7c8f2a5c16be9f27131f1b4df1d806 (diff)
parent404818aba58b42378be8739a238117188070d5af (diff)
downloadperlweeklychallenge-club-3753f54f4fc4628cf6292c5f79f76654b64e46c8.tar.gz
perlweeklychallenge-club-3753f54f4fc4628cf6292c5f79f76654b64e46c8.tar.bz2
perlweeklychallenge-club-3753f54f4fc4628cf6292c5f79f76654b64e46c8.zip
Merge pull request #577 from noudald/challenge-023-noud
Solutions to challenge 023 problem 1 and 2 in Perl 6 by Noud
-rw-r--r--challenge-023/noud/perl6/ch-1.p619
-rw-r--r--challenge-023/noud/perl6/ch-2.p613
2 files changed, 32 insertions, 0 deletions
diff --git a/challenge-023/noud/perl6/ch-1.p6 b/challenge-023/noud/perl6/ch-1.p6
new file mode 100644
index 0000000000..eebaa28635
--- /dev/null
+++ b/challenge-023/noud/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.
+
+sub grad(@arr, $step=1) {
+ (1..(@arr.elems - $step)).map({ @arr[$_ + $step] - @arr[$_] });
+}
+
+sub MAIN(Int $order, *@arr) {
+ grad(@arr, $order).say;
+}
diff --git a/challenge-023/noud/perl6/ch-2.p6 b/challenge-023/noud/perl6/ch-2.p6
new file mode 100644
index 0000000000..bbc995b5c0
--- /dev/null
+++ b/challenge-023/noud/perl6/ch-2.p6
@@ -0,0 +1,13 @@
+# 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.
+
+sub decomp(Int $n) {
+ if ($n > 1) {
+ my $prime = (2..Inf).grep({ $n %% $_ })[0];
+ ($prime, |(decomp(($n / $prime).Int)));
+ }
+}
+
+decomp(228).say;