aboutsummaryrefslogtreecommitdiff
path: root/challenge-023
diff options
context:
space:
mode:
authorNoud Aldenhoven <noud.aldenhoven@gmail.com>2019-08-31 15:36:30 +0200
committerNoud Aldenhoven <noud.aldenhoven@gmail.com>2019-08-31 15:36:30 +0200
commit404818aba58b42378be8739a238117188070d5af (patch)
tree83fa5dda3f05d9c034c9b9b118a12b708b804604 /challenge-023
parent603db09fe93651972fe09d670d1a8e14f9806235 (diff)
downloadperlweeklychallenge-club-404818aba58b42378be8739a238117188070d5af.tar.gz
perlweeklychallenge-club-404818aba58b42378be8739a238117188070d5af.tar.bz2
perlweeklychallenge-club-404818aba58b42378be8739a238117188070d5af.zip
Solutions to challenge 023 problem 1 and 2 in Perl 6 by Noud
Diffstat (limited to 'challenge-023')
-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;