-
Notifications
You must be signed in to change notification settings - Fork 0
/
10006.cpp
64 lines (52 loc) · 1.4 KB
/
10006.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
62
63
64
#include <cstdio>
using namespace std;
unsigned int check(unsigned int base, unsigned int power, unsigned int divid)
{
unsigned int left;
base %= divid;
if (power == 1)
left = base;
else if (power == 2)
left = (base * base) % divid;
else if (power % 2)
left = (check(base, power - 1, divid) * base) % divid;
else
left = (check(base * base, power / 2, divid)) % divid;
return left;
}
int main(void)
{
bool arr[65000], flag;
unsigned int n, i, len, list[32500], beg, end;
for (i = 2; i < 65000; ++i)
arr[i] = true;
for (i = 2; i < 65000; ++i)
if (arr[i])
for (n = 2 * i; n < 65000; n += i)
arr[n] = false;
len = 0;
for (i = 2; i < 65000; ++i)
if (arr[i]) list[len++] = i;
while (scanf("%d", &n) != EOF) {
if (n < 1) break;
beg = 0;
end = len;
while (end - beg > 1) {
i = (beg + end) / 2;
if (list[i] > n)
end = i;
else if (list[i] < n)
beg = i;
else
break;
}
flag = list[i] != n;
for (i = 2; flag && i < n; ++i)
if (check(i, n, n) != i) flag = false;
if (flag)
printf("The number %d is a Carmichael number.\n", n);
else
printf("%d is normal.\n", n);
}
return 0;
}