feat: add phi

This commit is contained in:
2025-07-18 00:00:00 +08:00
parent d074410004
commit 5ac2a0a8ad

View 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;
}
}