-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEULER-35.cpp
More file actions
63 lines (55 loc) · 1.53 KB
/
EULER-35.cpp
File metadata and controls
63 lines (55 loc) · 1.53 KB
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
// If you want to know how I got this solution, please email me at hi@webcoder49.dev .
#include <bits/stdc++.h>
using namespace std;
class PrimeFinder {
vector<int> _primes;
int _generatePrimesFrom = 2;
public:
void generatePrimes(int maxSquared) {
int num;
for(num = _generatePrimesFrom; num*num <= maxSquared; num++) {
if(isPrime(num)) {
_primes.push_back(num);
}
}
_generatePrimesFrom = num;
}
bool isPrime(int num) {
if(num < _generatePrimesFrom) {
return find(_primes.begin(), _primes.end(), num) != _primes.end();
}
generatePrimes(num); // Up to square root so one from each pair of factors.
for(int i = 0; i < _primes.size(); i++) {
if(num % _primes[i] == 0) return false; // Not prime as divisible
if(_primes[i]*_primes[i] > num) break; // Don't need to look further than sqrt
}
return true;
}
bool isCircularPrime(int num) {
if(!isPrime(num)) {
return false;
}
string numStr = to_string(num);
for(int i = 0; i < numStr.size(); i++) {
// Rotate string
numStr = numStr.substr(1) + numStr[0];
int rotation = 0;
rotation = stoi(numStr);
if(!isPrime(rotation)) {
return false;
}
}
return true;
}
};
int main() {
PrimeFinder primeFinder;
int nextMin = 2;
int count = 0;
for(int i = 2; i < 1000000; i++) {
if(primeFinder.isCircularPrime(i)) {
count++;
}
}
cout << "Count:" << count << "\n";
}