-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fermat.java
37 lines (27 loc) · 1.14 KB
/
Fermat.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
import java.math.BigInteger;
import java.util.Scanner;
public class Fermat {
public static void FermatForPrimalityTest(BigInteger number) {
// Boolean variable to store the result
boolean result;
// When certainty is one,
// it will check number for prime or composite
result = number.isProbablePrime(1);
System.out.println(number.toString() + " with certainty 1 " + result);
// When certainty is zero,
// it is always true
result = number.isProbablePrime(0);
System.out.println(number.toString() + " with certainty 0 " + result);
// When certainty is negative,
// it is always true
result = number.isProbablePrime(-1);
System.out.println(number.toString() + " with certainty -1 " + result);
}
public static void main(String[] args)
{
Scanner scn = new Scanner(System.in);
System.out.println("Enter a number to test primality : ");
BigInteger number = scn.nextBigInteger();
FermatForPrimalityTest(number);
}
}