-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEulersTheorem.java
More file actions
44 lines (43 loc) · 1.32 KB
/
EulersTheorem.java
File metadata and controls
44 lines (43 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.Scanner;
public class EulersTheorem {
static int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
static int eulerTotient(int n) {
int result = n;
for (int p = 2; p * p <= n; p++) {
if (n % p == 0) {
while (n % p == 0) n /= p;
result -= result / p;
}
}
if (n > 1) result -= result / n;
return result;
}
static long modExp(int a, int b, int m) {
long result = 1;
a %= m;
while (b > 0) {
if ((b & 1) == 1) result = (result * a) % m;
b >>= 1;
a = (a * a) % m;
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter integer a: ");
int a = scanner.nextInt();
System.out.print("Enter integer n: ");
int n = scanner.nextInt();
if (gcd(a, n) != 1) {
System.out.println("a and n are not coprime. Euler's theorem does not apply.");
} else {
int phiN = eulerTotient(n);
System.out.println("φ(" + n + ") = " + phiN);
long result = modExp(a, phiN, n);
System.out.println("a^φ(n) mod n = " + result);
}
scanner.close();
}
}