aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-159/jo-37/perl/ch-1.pl82
-rwxr-xr-xchallenge-159/jo-37/perl/ch-2.pl95
2 files changed, 177 insertions, 0 deletions
diff --git a/challenge-159/jo-37/perl/ch-1.pl b/challenge-159/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..fef0017acb
--- /dev/null
+++ b/challenge-159/jo-37/perl/ch-1.pl
@@ -0,0 +1,82 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use Syntax::Keyword::Gather;
+use experimental 'signatures';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [N]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N
+ print the Farey sequence for N
+
+EOS
+
+
+### Input and Output
+
+say for farey_sequence(shift);
+
+
+### Implementation
+
+# Adopted from the Wikipedia article.
+sub farey_sequence ($k) {
+
+ gather {
+ my ($n_p, $d_p, $n, $d) = (0, 1, 1, $k);
+ take "$n_p/$d_p";
+ take "$n/$d";
+ while ($d > 1) {
+ # Calculate the new numerator and denominator from the
+ # previous values.
+ my $f = int +($d_p + $k) / $d;
+ ($n_p, $d_p, $n, $d) = ($n, $d, $f * $n - $n_p, $f * $d - $d_p);
+ take "$n/$d";
+ }
+ };
+}
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is scalar(farey_sequence(5)),
+ [qw(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 scalar(farey_sequence(7)),
+ [qw(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 scalar(farey_sequence(4)),
+ [qw(0/1 1/4 1/3 1/2 2/3 3/4 1/1)],
+ 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is scalar(farey_sequence(8)),
+ [qw(0/1 1/8 1/7 1/6 1/5 1/4 2/7 1/3 3/8 2/5 3/7 1/2 4/7 3/5
+ 5/8 2/3 5/7 3/4 4/5 5/6 6/7 7/8 1/1)],
+ 'From Wikipedia';
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-159/jo-37/perl/ch-2.pl b/challenge-159/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..e5e1313189
--- /dev/null
+++ b/challenge-159/jo-37/perl/ch-2.pl
@@ -0,0 +1,95 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use Math::Utils qw(fsum gcd ceil);
+use Math::Trig 'pi2';
+
+# For testing only:
+use Math::Prime::Util;
+
+use experimental 'signatures';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [N]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N
+ Print µ(N).
+
+EOS
+
+
+### Input and Output
+
+# Round to integer.
+printf "%.0f\n", moebius(shift);
+
+
+### Implementation
+
+# The Möbius function is provided by the awesome Math::Prime::Util.
+# Using it for cross-checking another implementation taken from
+# Wikipedia:
+#
+# µ(n) equals the sum of all primitive n-th roots of unity.
+# Though this is really funny, it's rather inefficient.
+#
+# Some considerations:
+# - There are no primitive roots with a zero imaginary part for n > 2.
+# - If a number z is a n-th root, then the conjugate number z* is a root,
+# too. And it is a different number for nonzero imaginary parts.
+# - From
+# z + z* = 2 Re z
+# it follows, that positive and negative imaginary parts cancel out
+# and the real parts double.
+# Thus it is sufficient to take twice the sum of the real parts of
+# primitive roots with a positive imaginary part. No complex arithmetic
+# is required.
+#
+# Remember:
+# exp(2πi k/n) are the n-th roots of unity and
+# exp(iϑ) = cos(ϑ) + i sin(ϑ)
+
+sub moebius ($n) {
+ # Treat the special cases
+ return 1 if $n == 1;
+ return -1 if $n == 2;
+
+ 2 * fsum map cos(pi2 * $_ / $n),
+ grep gcd($_, $n) == 1, 1 .. ceil($n / 2) - 1;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is moebius(5), float(-1), 'example 1';
+ is moebius(10), float(1), 'example 2';
+ is moebius(20), float(0), 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ for my $n (1 .. 128) {
+ is moebius($n),
+ float(Math::Prime::Util::moebius($n)), "n=$n";
+ }
+ }
+
+ done_testing;
+ exit;
+}