-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem0049.cpp
70 lines (53 loc) · 1.15 KB
/
problem0049.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
65
66
67
68
69
70
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstring>
#define MAX 10000
using namespace std;
bool isPermutation(int, int);
void handleMultiples(int, bool[]);
int nextPrime(int, bool[]);
int main(void){
bool primes[MAX];
memset(primes, true, sizeof(primes));
primes[0] = false;
primes[1] = false;
int prime = 2;
for(int i = 2; i < sqrt(MAX) + 1; i++){
handleMultiples(prime, primes);
prime = nextPrime(prime, primes);
}
int limit = 9999 - 6660;
for(int i = 0; i < limit; i++){
if(primes[i] && primes[i+3330] && primes[i+6660] && isPermutation(i, i + 3330) && isPermutation(i, i + 6660))
cout << i << i + 3330 << i + 6660 << endl;
}
}
void handleMultiples(int prime, bool primes[]){
for(int i = prime * prime; i < MAX; i+=prime){
primes[i] = false;
}
}
int nextPrime(int prime, bool primes[]){
prime++;
while(!primes[prime]){
prime++;
}
return prime;
}
bool isPermutation(int a, int b){
int occur[10];
memset(occur, 0, sizeof(occur));
while(a > 0){
occur[a%10]++;
a /= 10;
}
while(b > 0){
occur[b%10]--;
b /= 10;
}
for(int i = 0; i < 10; i++){
if(occur[i] != 0) return false;
}
return true;
}