aboutsummaryrefslogtreecommitdiff
path: root/challenge-020
diff options
context:
space:
mode:
authorNoud Aldenhoven <noud.aldenhoven@gmail.com>2019-08-10 16:27:54 +0200
committerNoud Aldenhoven <noud.aldenhoven@gmail.com>2019-08-10 16:27:54 +0200
commit02524164e2ab8edb1cc5df7564037369e2aa9ad7 (patch)
tree51b94e53adfc8095dde41fc24d69fc07e2e41d95 /challenge-020
parentcdeda343bd6f44b567fc100cece604b721e7f4ea (diff)
downloadperlweeklychallenge-club-02524164e2ab8edb1cc5df7564037369e2aa9ad7.tar.gz
perlweeklychallenge-club-02524164e2ab8edb1cc5df7564037369e2aa9ad7.tar.bz2
perlweeklychallenge-club-02524164e2ab8edb1cc5df7564037369e2aa9ad7.zip
Solutions to challenge 20 problem 1 and 2 in Perl 6 by Noud
Diffstat (limited to 'challenge-020')
-rw-r--r--challenge-020/noud/perl6/ch-1.p613
-rw-r--r--challenge-020/noud/perl6/ch-2.p622
2 files changed, 35 insertions, 0 deletions
diff --git a/challenge-020/noud/perl6/ch-1.p6 b/challenge-020/noud/perl6/ch-1.p6
new file mode 100644
index 0000000000..bf8b4259f0
--- /dev/null
+++ b/challenge-020/noud/perl6/ch-1.p6
@@ -0,0 +1,13 @@
+# Write a script to accept a string from command line and split it on change of
+# character. For example, if the string is “ABBCDEEF”, then it should split
+# like “A”, “BB”, “C”, “D”, “EE”, “F”.
+
+sub MAIN(Str $str="ABBCDEEF") {
+ map({$_.Str.say}, $str ~~ m:g/(.)$0*/);
+}
+
+# Regular expression explained:
+#
+# m:g --> Match global adverb, i.e. match repeatedly.
+# (.) --> Match one character (with variable name $0).
+# $0* --> Match zero or more times character $0.
diff --git a/challenge-020/noud/perl6/ch-2.p6 b/challenge-020/noud/perl6/ch-2.p6
new file mode 100644
index 0000000000..43e846f267
--- /dev/null
+++ b/challenge-020/noud/perl6/ch-2.p6
@@ -0,0 +1,22 @@
+# Write a script to print the smallest pair of Amicable Numbers. For more
+# information, please checkout wikipedia page.
+
+
+sub prop_div(Int $i) {
+ return [+] (1..^$i).grep($i %% *);
+}
+
+# Anonymous cache handler. Trick copied from fjwhittle (ch-017, ch-1.p6).
+&prop_div.wrap: -> $i { .[$i] //= callsame } given Array.new;
+
+
+# Create a lazy list of all Amicable Numbers.
+my @amicables = lazy gather for 1..^Inf -> $m {
+ for 1..^$m -> $n {
+ take ($n, $m) if prop_div($m) == $n and prop_div($n) == $m;
+ }
+}
+
+
+# Print the first three Amicable Numbers.
+$_.say for @amicables[^3];