aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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..574ef4e4db
--- /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)>>.&möbius, ( 1, -1, -1, 0, -1, 1, -1, 0, 0, 1);
+is-deeply (11..20)>>.&möbius, (-1, 0, -1, 1, 1, 0, -1, 0, -1, 0);
+
+sub möbius(\n)
+{
+ my @a = prime-factors(n);
+ return 0 if @a.squish < @a;
+ return @a %% 2 ?? 1 !! -1;
+}