-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10924.cpp
61 lines (55 loc) · 1.23 KB
/
P10924.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
#include <iostream>
#include <stdio.h>
#define PRIME_LEN 500000
class PrimeHandler {
bool primes[PRIME_LEN];
public:
void init() {
for(int i = 0; i < PRIME_LEN; ++i)
primes[i] = true;
// Sieve primes:
for(int i = 0; i*i < PRIME_LEN; ++i) {
if(!primes[i])
continue;
// Mark all uneven multiples as non-prime:
int basePrime = 1+2*(i+1);
for(int multiple = 3; true; multiple += 2) {
int notAPrime = basePrime*multiple;
int notAPrimeI = notAPrime/2-1;
if(notAPrimeI >= PRIME_LEN)
break;
primes[notAPrimeI] = false;
}
}
}
bool isPrime(int n) const {
if(n == 2 || n == 1) // WARNING: 1 IS NOT A PRIME!
return true;
if(n < 2 || (n%2==0))
return false;
return primes[n/2-1];
}
};
int main() {
// Compute prime numbers:
PrimeHandler ph;
ph.init();
char w[250];
while(gets(w)) {
int sum = 0;
char c;
for(int i = 0; isprint(c = w[i]); ++i) {
if(c >= 'a' && c <= 'z') {
sum += (c-'a')+1;
}
else if(c >= 'A' && c <= 'Z') {
sum += (c-'A')+27;
}
}
if(ph.isPrime(sum))
std::cout << "It is a prime word." << std::endl;
else
std::cout << "It is not a prime word." << std::endl;
}
return 0;
}