aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-159/e-choroba/perl/ch-1.pl40
-rwxr-xr-xchallenge-159/e-choroba/perl/ch-2.pl23
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-159/e-choroba/perl/ch-1.pl b/challenge-159/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..24e047cd84
--- /dev/null
+++ b/challenge-159/e-choroba/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental 'signatures';
+
+use Math::BigRat;
+
+sub mediant ($p, $q, $depth) {
+ my $m = Math::BigRat->new($p->numerator + $q->numerator)
+ / ($p->denominator + $q->denominator);
+ return $m->denominator <= $depth ? $m : undef
+}
+
+sub farey_sequence ($n) {
+ my @farey = map 'Math::BigRat'->new($_), '0/1', '1/1';
+ for my $depth (2 .. $n) {
+ for (my $i = 0; $i < $#farey; ++$i) {
+ if (my $m = mediant($farey[$i], $farey[1 + $i], $depth)) {
+ splice @farey, ++$i, 0, $m;
+ }
+ }
+ }
+ @farey[0, -1] = ('0/1', '1/1');
+ return \@farey
+}
+
+use Test::More tests => 3;
+
+is_deeply 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_deeply 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_deeply farey_sequence(4),
+ [qw[ 0/1 1/4 1/3 1/2 2/3 3/4 1/1 ]],
+ 'Example 3';
diff --git a/challenge-159/e-choroba/perl/ch-2.pl b/challenge-159/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..500351616d
--- /dev/null
+++ b/challenge-159/e-choroba/perl/ch-2.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental 'signatures';
+
+use List::Util qw{ uniq };
+use Math::Prime::Util qw{ factor };
+
+sub moebius_number ($n) {
+ my @f = factor($n);
+
+ return 0 if @f != uniq(@f);
+ return -1 if @f % 2;
+ return 1
+}
+
+use Test::More tests => 4;
+
+is moebius_number(5), -1, 'Example 1';
+is moebius_number(10), 1, 'Example 2';
+is moebius_number(20), 0, 'Example 3';
+
+is moebius_number(100), 0, 'Even number of prime factors but square';