aboutsummaryrefslogtreecommitdiff
path: root/challenge-159/mark-anderson
diff options
context:
space:
mode:
authorMark <53903062+andemark@users.noreply.github.com>2022-04-04 06:34:44 +0000
committerMark <53903062+andemark@users.noreply.github.com>2022-04-04 06:34:44 +0000
commitc748d446c61a5f4c1923e88b09bebb75c26997c2 (patch)
tree5be9789537ac37ce268996a9f18055d63de54774 /challenge-159/mark-anderson
parent21b2771f7439710ee9a4631c40679a916ca9f723 (diff)
downloadperlweeklychallenge-club-c748d446c61a5f4c1923e88b09bebb75c26997c2.tar.gz
perlweeklychallenge-club-c748d446c61a5f4c1923e88b09bebb75c26997c2.tar.bz2
perlweeklychallenge-club-c748d446c61a5f4c1923e88b09bebb75c26997c2.zip
Challenge 159 Solutions (Raku)
Diffstat (limited to 'challenge-159/mark-anderson')
-rw-r--r--challenge-159/mark-anderson/raku/ch-1.raku24
-rw-r--r--challenge-159/mark-anderson/raku/ch-2.raku14
2 files changed, 38 insertions, 0 deletions
diff --git a/challenge-159/mark-anderson/raku/ch-1.raku b/challenge-159/mark-anderson/raku/ch-1.raku
new file mode 100644
index 0000000000..652d28a6af
--- /dev/null
+++ b/challenge-159/mark-anderson/raku/ch-1.raku
@@ -0,0 +1,24 @@
+#!/usr/bin/env raku
+
+use Test;
+
+is-deeply farey-seq(5), (0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1), 'example 1';
+
+is-deeply farey-seq(7), (0/1, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 2/5, 3/7, 1/2, 4/7, 3/5, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 1/1), 'example 2';
+
+is-deeply farey-seq(4), (0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1), 'example 3';
+
+is farey-seq(100).elems, 3045, 'https://oeis.org/A005728/b005728.txt';
+
+sub farey-seq(\n)
+{
+ return 0/1, 1/n, { next-term($^f, $^s) } ... 1/1;
+
+ sub next-term(\f, \s)
+ {
+ my (\a, \b) = f.nude;
+ my (\c, \d) = s.nude;
+ my \k = (n + b) div d;
+ (k * c - a) / (k * d - b)
+ }
+}
diff --git a/challenge-159/mark-anderson/raku/ch-2.raku b/challenge-159/mark-anderson/raku/ch-2.raku
new file mode 100644
index 0000000000..5d69eda71d
--- /dev/null
+++ b/challenge-159/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,14 @@
+#!/usr/bin/env raku
+
+use Prime::Factor;
+use Test;
+
+is-deeply ( 1..10)>>.&mobius, ( 1, -1, -1, 0, -1, 1, -1, 0, 0, 1);
+is-deeply (11..20)>>.&mobius, (-1, 0, -1, 1, 1, 0, -1, 0, -1, 0);
+
+sub mobius(\n)
+{
+ my @a = prime-factors(n);
+ return 0 if @a.squish < @a;
+ return @a %% 2 ?? 1 !! -1;
+}