diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-04-11 00:52:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-11 00:52:22 +0100 |
| commit | 5083cba2ff2a797b16a69242b34bdae8fd470cdb (patch) | |
| tree | 54b09c0f884acdffa0ad082ae5f3157c76760369 /challenge-159/cheok-yin-fung/java/Moebius.java | |
| parent | a46015a3327c391ba7e80ea2c095dae83ac65a4d (diff) | |
| parent | 44249e2cf239bd2fb9ae3014b8bd0ab330ea4ef6 (diff) | |
| download | perlweeklychallenge-club-5083cba2ff2a797b16a69242b34bdae8fd470cdb.tar.gz perlweeklychallenge-club-5083cba2ff2a797b16a69242b34bdae8fd470cdb.tar.bz2 perlweeklychallenge-club-5083cba2ff2a797b16a69242b34bdae8fd470cdb.zip | |
Merge pull request #5911 from E7-87-83/newt
Week 159
Diffstat (limited to 'challenge-159/cheok-yin-fung/java/Moebius.java')
| -rw-r--r-- | challenge-159/cheok-yin-fung/java/Moebius.java | 79 |
1 files changed, 79 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; + } +} |
