forked from TheAlgorithms/Java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFermatLittleTheorem.java
More file actions
Latest commit
51 lines (35 loc) · 658 Bytes
/
Copy pathFermatLittleTheorem.java
File metadata and controls
51 lines (35 loc) · 658 Bytes
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
45
46
47
48
classFermatLittle
{
staticint__gcd(inta, intb)
{
if(b == 0)
{
returna;
}
else
{
return__gcd(b, a % b);
}
}
staticintpower(intx,inty,intm)
{
if (y == 0)
return1;
intp = power(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
staticvoidmodInverse(inta, intm)
{
if (__gcd(a, m) != 1)
System.out.print("Inverse doesn't exist");
else {
System.out.print("Modular multiplicative inverse is "+power(a, m - 2, m));
}
}
publicstaticvoidmain (String[] args)
{
inta = 3, m = 11;
modInverse(a, m);
}
}