-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsprime.cpp
More file actions
55 lines (43 loc) · 1.04 KB
/
sprime.cpp
File metadata and controls
55 lines (43 loc) · 1.04 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
/*
ID: th3c0r11
TASK: sprime
LANG: C++14
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;
int N;
long primes[8];
// level of failure. N - 1: one-digit number is non-prime up to 0: entire number is non-prime
int isSuperprime(long l)
{
for (size_t i = 0; i < N; ++i, l /= 10)
primes[i] = l;
if (primes[N - 1] == 1 || primes[N - 1] == 4 || primes[N - 1] == 6 || primes[N - 1] == 8 || primes[N - 1] == 9)
return N - 1;
for (size_t i = N - 1; i >= 1; --i) {
for (long d = 2; d <= sqrt(primes[i - 1]); ++d)
if (primes[i - 1] % d == 0)
return i - 1;
}
return -1;
}
int main()
{
ifstream in{"sprime.in"};
ofstream out{"sprime.out"};
in >> N;
long max = pow(10, N);
for (long i = pow(10, N - 1) + 1; i < max; ) {
int level = isSuperprime(i);
if (level == -1) {
out << i << '\n';
i += 2;
} else {
i += pow(10, level);
}
}
}