-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprime.cpp
executable file
·45 lines (41 loc) · 897 Bytes
/
sprime.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
/*
ID: paulius10
PROG: sprime
LANG: C++
*/
#include <fstream>
#include <queue>
#include <math.h>
using namespace std;
bool isPrime(int n) {
for (int d = 2; d * d <= n; d++) {
if (n % d == 0) return false;
}
return true;
}
int main() {
ifstream fin("sprime.in");
int N;
fin >> N;
fin.close();
int primeDigits[] = {2, 3, 5, 7};
queue<int> primes;
for (int i = 0; i < 4; i++) primes.push(primeDigits[i]);
ofstream fout("sprime.out");
while (!primes.empty()) {
int prime = primes.front();
primes.pop();
int digitsCount = (int) log10((double) prime) + 1;
if (digitsCount > N) break;
if (digitsCount == N) {
fout << prime << endl;
} else {
for (int digit = 0; digit <= 9; digit++) {
int newPrime = prime * 10 + digit;
if (isPrime(newPrime)) primes.push(newPrime);
}
}
}
fout.close();
return 0;
}