forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFermat_Little_Theorem.java
More file actions
56 lines (50 loc) · 1.62 KB
/
Fermat_Little_Theorem.java
File metadata and controls
56 lines (50 loc) · 1.62 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
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
x^(n-1) cong== 1modn
where x and n are coprime (gcd is 1)
multiplying by x inverse on both sides
x inverse = x^(n-2) mod n which is found using fast modular exponentiation
*/
public class Fermat_Little_Theorem {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int x;
int n;
System.out.println("Enter space separated coprime numbers x and n");
String input[] = br.readLine().split(" ");
x = Integer.parseInt(input[0]);
n = Integer.parseInt(input[1]);
findModInverse(x,n);
}
public static void findModInverse(int x, int n){
if(recursiveGCD(x,n)!=1){
System.out.println("inverse does not exist! returning...");
return;
}
else{
System.out.println("Modular Multiplicative inverse is..."+ fastModuloExponentiation(x,n-2,n));
}
}
public static int recursiveGCD(int a, int b){
return a==0 ? b:recursiveGCD(b%a,a);
}
public static long fastModuloExponentiation(int number, int power, int modulus){
if(power==0)
return 1;
if(power==1)
return number;
long sub_number = fastModuloExponentiation(number,power/2,modulus)%modulus;
if(power%2==0)
return (sub_number*sub_number)%modulus;
else
return ((sub_number*sub_number)%modulus * number%modulus)%modulus;
}
}
/*
input :
3 11
output :
Modular Multiplicative inverse is...4
*/