From 7a219f03ce9b654277078bfb906cbc0741c9e3ba Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 4 Apr 2022 18:45:36 +0200 Subject: Solution to task 1 --- challenge-159/jo-37/perl/ch-1.pl | 82 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 challenge-159/jo-37/perl/ch-1.pl 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 < 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; +} -- cgit From 41e99f11d4d91ffa5f40e5f0f171cc406b1fb055 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 4 Apr 2022 18:45:50 +0200 Subject: Solution to task 2 --- challenge-159/jo-37/perl/ch-2.pl | 95 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100755 challenge-159/jo-37/perl/ch-2.pl 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 < 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; +} -- cgit