-
Notifications
You must be signed in to change notification settings - Fork 36
/
chapter10-primality-testing.cpp
61 lines (50 loc) · 920 Bytes
/
chapter10-primality-testing.cpp
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
57
58
59
60
61
// Primality testing using Fermat's Lesser Theorem.
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int primalityTest(int a, int i, int n)
{
// a is randomly chosen between (1, n) to test the primality of n.
int x, y;
if (i == 0) {
return 1;
}
x = primalityTest(a, i / 2, n);
if (x == 0) {
// x == 0 means n is composite.
// recursively return false
return 0;
}
y = (x * x) % n;
if (i % 2 != 0) {
y = (y * a) % n;
}
return y;
}
bool isPrime(const int n)
{
if (n < 2) {
return false;
} else if (n < 4) {
return true;
}
const int NUM_OF_TRIALS = 10;
int i = 0;
while (i < NUM_OF_TRIALS) {
if (primalityTest(2 + rand() % (n - 3), n - 1, n) != 1) {
return false;
}
++i;
}
return true;
}
int main()
{
srand((unsigned int)time(nullptr));
int n;
while (cin>> n && n > 0) {
cout << (isPrime(n) ? "Yes": "No") << endl;
}
return 0;
}