-
Notifications
You must be signed in to change notification settings - Fork 65
/
10006 Carmichael Numbers.cpp
44 lines (39 loc) · 1.09 KB
/
10006 Carmichael Numbers.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
#include <iostream>
#include <vector>
using namespace std;
// First, filter for primes
// Then, for each num, for every n greater than it, test if the carmichael doesn't hold
// This is sqrt(65005)
const int PrimeNum = 256;
const int MaxNumChecked = 65005;
bool isComp[MaxNumChecked];
bool isCar[MaxNumChecked];
int main()
{
// Calculated using Korselt's criterion
for (int i = 3; i < MaxNumChecked; i += 2)
isCar[i] = true;
for (int i = 3; i < MaxNumChecked; i += 2)
{
// Means is still prime
if (!isComp[i])
{
isCar[i] = false;
for (int j = 3 * i; j < MaxNumChecked; j += 2*i)
{
isComp[j] = true;
if ((j / i) % i == 0 || // Contains the factor i twice
(j - 1) % (i - 1) != 0) // i - 1 | j - 1 is not true
isCar[j] = false;
}
}
}
int n;
while (cin >> n, n)
{
if (isCar[n])
cout << "The number " << n << " is a Carmichael number.\n";
else
cout << n << " is normal.\n";
}
}