-
-
Notifications
You must be signed in to change notification settings - Fork 611
/
FermatPrimality.java
47 lines (43 loc) · 1.61 KB
/
FermatPrimality.java
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
package algo.numerals;
/**
* Created by sherxon on 1/13/17.
*/
/**
* This is method to check if number is prime or not.
* Fermat’s “little theorem” states that if p is prime and 1 ≤ n ≤ p, np–1 Mod
* p = 1. In other words, if you raise n to the p–1 power and then take the result
* modulo p, the result is 1.
* For example, suppose p = 11 and n = 2. Then np–1 Mod p = 210 Mod 11 = 1,024
* Mod 11. The value 1,024 = 11 × 93 + 1, so 1,024 Mod 11 = 1 as desired.
* Note that it is possible for np–1 Mod p = 1 even if p is not prime. In that case
* the value n is called a Fermat liar because it incorrectly implies that p is prime.
* If np–1 Mod p ≠ 1, n is called a Fermat witness because it proves that p is
* not prime
* We can use many test to remove Fermat liar. For example, if p passes the test 10 times, there is a 1/210 ≈ 0.00098 probability
* that p is not prime
*/
public class FermatPrimality {
public static void main(String[] args) {
System.out.println(isPrime(97, 10));
}
static boolean isPrime(int p, int testNum) {
for (int i = 0; i < testNum; i++) {
int n = (int) (Math.random() * p) + 1; // min should be 1
if (mod(n, p - 1, p) != 1) {
System.out.println(n);
return false;
}
}
//There is a 1/testNum chance that it is not prime.
return true;
}
// (n ^ (p-1)) % p
private static int mod(int n, int i, int p) {
int res = 1;
for (int j = 0; j < i; j++) {
res *= n;
res %= p;
}
return res % p;
}
}