From 5ac2a0a8ad6708bde4b3f5477626bc911c4baf1f Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Fri, 18 Jul 2025 00:00:00 +0800 Subject: [PATCH] feat: add phi --- src/main/java/me/hatter/math/Phi.java | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/java/me/hatter/math/Phi.java diff --git a/src/main/java/me/hatter/math/Phi.java b/src/main/java/me/hatter/math/Phi.java new file mode 100644 index 0000000..66dcbf1 --- /dev/null +++ b/src/main/java/me/hatter/math/Phi.java @@ -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; + } +}