feat: add phi
This commit is contained in:
28
src/main/java/me/hatter/math/Phi.java
Normal file
28
src/main/java/me/hatter/math/Phi.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package me.hatter.math;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class Phi {
|
||||
|
||||
public static void main(String[] args) {
|
||||
for (long x = 4; x < 10000; x++) {
|
||||
// gcd(3, x) = 1
|
||||
if (BigInteger.valueOf(3).gcd(BigInteger.valueOf(x)).equals(BigInteger.ONE)) {
|
||||
// 3 ^ phi(x) mod x = 1
|
||||
System.out.println(BigInteger.valueOf(3).modPow(phi(BigInteger.valueOf(x)), BigInteger.valueOf(x)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static BigInteger phi(BigInteger n) {
|
||||
BigInteger p = BigInteger.valueOf(0);
|
||||
BigInteger i = BigInteger.valueOf(1);
|
||||
while (i.compareTo(n) < 0) {
|
||||
if (i.gcd(n).equals(BigInteger.ONE)) {
|
||||
p = p.add(BigInteger.ONE);
|
||||
}
|
||||
i = i.add(BigInteger.ONE);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user