aboutsummaryrefslogtreecommitdiff
path: root/challenge-159
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2022-04-11 02:18:14 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2022-04-11 02:18:14 +0800
commit7a083e3ad4201442badb8b6ff67f21cc2ec53469 (patch)
treeba84ec66f24d40400cc5dba4ec26d5129250a0bf /challenge-159
parentccacaa9af82ea74c7ad2b3518d4d3bb3012ae80c (diff)
downloadperlweeklychallenge-club-7a083e3ad4201442badb8b6ff67f21cc2ec53469.tar.gz
perlweeklychallenge-club-7a083e3ad4201442badb8b6ff67f21cc2ec53469.tar.bz2
perlweeklychallenge-club-7a083e3ad4201442badb8b6ff67f21cc2ec53469.zip
correct filing
Diffstat (limited to 'challenge-159')
-rw-r--r--challenge-159/cheok-yin-fung/java/Moebius.java79
-rw-r--r--challenge-159/cheok-yin-fung/julia/ch-2.jl28
-rw-r--r--challenge-159/cheok-yin-fung/perl/ch-1.pl45
-rw-r--r--challenge-159/cheok-yin-fung/perl/ch-2.pl63
4 files changed, 215 insertions, 0 deletions
diff --git a/challenge-159/cheok-yin-fung/java/Moebius.java b/challenge-159/cheok-yin-fung/java/Moebius.java
new file mode 100644
index 0000000000..b52e049ca8
--- /dev/null
+++ b/challenge-159/cheok-yin-fung/java/Moebius.java
@@ -0,0 +1,79 @@
+// The Weekly Challenge 159
+// Task 2 Moebius Function
+// Usage: java Moebius N
+
+public class Moebius
+{
+ public static void main(String[] args)
+ {
+ int N = 1;
+ try {
+ N = Integer.parseInt(args[0]);
+ } catch (Exception e) {
+ System.err.print("Please use a positive integer ");
+ System.err.println("as your parameter.");
+ System.exit(0);
+ }
+ double[][] complex1 = new double[][] { {1, 0} , {0, 1} };
+ double[][] complex2 = new double[][] { {0, 1} , {-1, 0} };
+ System.out.println(Math.round(mu(N)));
+ }
+
+
+ public static double mu(int n)
+ {
+ double[][] sum = ithRootOfUnityModuloN(1, n);
+ LOOP: for (int i=2; i<n; i++)
+ {
+ for (int s=2; s<n; s++)
+ {
+ if ( (i*s) % n == 0 )
+ continue LOOP;
+ }
+ sum = complexAddition(sum, ithRootOfUnityModuloN(i,n));
+ }
+ return sum[0][0];
+ }
+
+
+ public static double[][] complexAddition(double[][] c1, double[][] c2)
+ {
+ double a = c1[0][0];
+ double b = c1[0][1];
+ double c = c1[1][0];
+ double d = c1[1][1];
+ double e = c2[0][0];
+ double f = c2[0][1];
+ double g = c2[1][0];
+ double h = c2[1][1];
+ return new double[][] {{ a+e, b+f }, { c+g, d+h }};
+ }
+
+
+
+ public static double[][] complexMultiplication(double[][] c1, double[][] c2)
+ {
+ double a = c1[0][0];
+ double b = c1[0][1];
+ double c = c1[1][0];
+ double d = c1[1][1];
+ double e = c2[0][0];
+ double f = c2[0][1];
+ double g = c2[1][0];
+ double h = c2[1][1];
+ return new double[][] {{a*e+b*g, a*f+b*h}, {c*e+d*g, c*f+d*h}};
+ }
+
+
+
+ public static double[][] ithRootOfUnityModuloN(int i , int n)
+ {
+ double realPart = Math.cos(2*Math.PI*i/n);
+ double imaginaryPart = Math.sin(2*Math.PI*i/n);
+ double[][] result = new double[][] {
+ {realPart, imaginaryPart},
+ {-imaginaryPart, realPart}
+ };
+ return result;
+ }
+}
diff --git a/challenge-159/cheok-yin-fung/julia/ch-2.jl b/challenge-159/cheok-yin-fung/julia/ch-2.jl
new file mode 100644
index 0000000000..a726b3b3f0
--- /dev/null
+++ b/challenge-159/cheok-yin-fung/julia/ch-2.jl
@@ -0,0 +1,28 @@
+
+
+function irn(i,n)
+ return cos(2π*i/n)+sin(2π*i/n)im
+end
+
+
+
+function möbius(n)
+ if n==1
+ return 1
+ end
+ if n==2
+ return -1
+ end
+ primitive_roots=Any[]
+ push!(primitive_roots, irn(1, n))
+ for i in 2:n-1
+ for s in 2:n-1
+ if (i*s)%n==0
+ @goto label1
+ end
+ end
+ push!(primitive_roots, irn(i,n))
+ @label label1
+ end
+ return round(Int,real(sum(primitive_roots)))
+end
diff --git a/challenge-159/cheok-yin-fung/perl/ch-1.pl b/challenge-159/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..d6500f2c5f
--- /dev/null
+++ b/challenge-159/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+# The Weekly Challenge 159
+# Task 1 Farey Sequence
+
+# https://en.wikipedia.org/wiki/Farey_sequence#Next_term
+
+use v5.22.0;
+use warnings;
+use POSIX;
+
+my $N = $ARGV[0] if defined($ARGV[0]);
+
+say farey($N) if defined($ARGV[0]);
+
+
+
+sub farey {
+ my $n = $_[0];
+ die "The parameter should be a positive integer." if $n==0;
+ my ($g, $h) = ([0,1], [1, $n]);
+ my @terms;
+ do {
+ push @terms, "".join("/", $g->@*);
+ my ($a, $b) = $g->@*;
+ my ($c, $d) = $h->@*;
+ my ($p, $q) = (
+ $c*floor (($n+$b)/$d) - $a,
+ $d*floor(($n+$b)/$d) - $b
+ );
+ ($g, $h) = ($h, [$p, $q]);
+ } while (!($h->[0] == 1 && $h->[1] == 1));
+ push @terms, join("/", $g->@*), join("/", $h->@*);
+ return join(", ",@terms) . ".";
+}
+
+
+
+use Test::More tests => 3;
+ok(
+ "0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1."
+ eq farey(5), "Example 1");
+ok(
+ "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." eq farey(7), "Example 2");
+ok("0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1." eq farey(4), "Example 3");
diff --git a/challenge-159/cheok-yin-fung/perl/ch-2.pl b/challenge-159/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..0d58c1eeb4
--- /dev/null
+++ b/challenge-159/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/perl
+# The Weekly Challenge 159
+# Task 2 Moebius Function
+# Usage: ch-2.pl $N
+use v5.22.0;
+use warnings;
+use Math::Complex;
+use List::Util qw/any/;
+use POSIX;
+
+use constant PI => 2*acos(0);
+
+
+if (defined($ARGV[0])) {
+ my $N = $ARGV[0];
+ say "mu($N) = ", mo($N);
+}
+
+
+sub irn {
+ my $i = $_[0];
+ my $n = $_[1];
+ return Math::Complex->make(
+ cos 2*PI*$i/$n, sin 2*PI*$i/$n
+ );
+}
+
+
+
+sub mo {
+ my $n = $_[0];
+ return 1 if $n == 1;
+ return -1 if $n == 2;
+ my $sum = irn(1, $n);
+ for my $i (2..$n-1) {
+ next if any { ($i*$_) % $n == 0} (2..$n-1);
+ $sum += irn($i, $n);
+ }
+ # say "# intermediate sum: ", Re($sum), "\n\n";
+ return floor(Re($sum)+0.5);
+}
+
+
+
+use Test::More tests => 18;
+ok mo(2) == -1, "test for 2";
+ok mo(5) == -1, "Example 1";
+ok mo(10) == 1, "Example 2";
+ok mo(20) == 0, "Example 3";
+ok mo(22) == 1, "test for 22";
+ok mo(41) == -1, "test for 41";
+ok mo(42) == -1, "test for 42";
+ok mo(43) == -1, "test for 43";
+ok mo(44) == 0, "test for 44";
+ok mo(45) == 0, "test for 44";
+ok mo(46) == 1, "test for 46";
+ok mo(47) == -1, "test for 47";
+ok mo(48) == 0, "test for 48";
+ok mo(49) == 0, "test for 49";
+ok mo(50) == 0, "test for 50";
+ok mo(3276) == 0, "test for 3276";
+ok mo(3277) == 1, "test for 3277";
+ok mo(3278) == -1, "test for 3278";