diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-12 18:56:29 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-12 18:56:29 +0100 |
| commit | 49f7f459092f538b5468e255ff4e8ebac490d70a (patch) | |
| tree | fa4a94a8207a58a56d4dca98ff22d39bed047f6e /challenge-159/cheok-yin-fung/java/Moebius.java | |
| parent | 1711da4f548d3134248cd5e02a13d71762d5e4cb (diff) | |
| parent | 72ba70a96cfd587443c3eaa0f5ba02e3557f4b82 (diff) | |
| download | perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.tar.gz perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.tar.bz2 perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.zip | |
Merge remote-tracking branch 'upstream/master'
# Conflicts:
# challenge-160/paulo-custodio/Makefile
Diffstat (limited to 'challenge-159/cheok-yin-fung/java/Moebius.java')
| -rw-r--r-- | challenge-159/cheok-yin-fung/java/Moebius.java | 81 |
1 files changed, 81 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..fbd6b0af30 --- /dev/null +++ b/challenge-159/cheok-yin-fung/java/Moebius.java @@ -0,0 +1,81 @@ +// 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]); + if (N<=0) + throw new ArithmeticException(); + } 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; + } +} |
