diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-04-05 13:56:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-05 13:56:21 +0100 |
| commit | 44f917580c2e96720674dd20a272f309eb1831f0 (patch) | |
| tree | 21c398359456336eeee0d150c4812ec8d3cfba0f | |
| parent | b6856191b0480cc201dab681863b2c72780f6bed (diff) | |
| parent | b29a31d9a01e0e46e68afff099655cedd88db78e (diff) | |
| download | perlweeklychallenge-club-44f917580c2e96720674dd20a272f309eb1831f0.tar.gz perlweeklychallenge-club-44f917580c2e96720674dd20a272f309eb1831f0.tar.bz2 perlweeklychallenge-club-44f917580c2e96720674dd20a272f309eb1831f0.zip | |
Merge pull request #5881 from andemark/branch-for-challenge-159
Challenge 159 Solutions (Raku)
| -rw-r--r-- | challenge-159/mark-anderson/raku/ch-1.raku | 24 | ||||
| -rw-r--r-- | challenge-159/mark-anderson/raku/ch-2.raku | 14 |
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; +} |
