-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConcatenatingPrimes.cpp
54 lines (49 loc) · 1.12 KB
/
ConcatenatingPrimes.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
#include<bits/stdc++.h>
using namespace std;
void printPrime(int n);
bool isPrime(int n);
unsigned int combination(unsigned int a, unsigned int b);
int main(){
int n;
cin >> n;
printPrime(n);
return 0;
}
bool isPrime(int n){
if (n <= 1)
return false;
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
unsigned int combination(unsigned int a, unsigned int b){
unsigned int x = 10;
while(b >= x)
x *= 10;
return a * x + b;
}
void printPrime(int n){
vector<int> primes;
for (int i = 2; i <= n; i++){
if (isPrime(i))
primes.push_back(i);
}
int totalN = primes.size();
set<int> combo;
for(int i=0; i<totalN; i++){
int x = primes.at(i);
for(int j=i+1; j<totalN; j++){
int y = primes.at(j);
int xy = combination(x, y);
int yx = combination(y, x);
if(isPrime(xy)){
combo.insert(xy);
}
if(isPrime(yx)){
combo.insert(yx);
}
}
}
cout << combo.size();
}