-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem 41
More file actions
37 lines (33 loc) · 787 Bytes
/
Copy pathproblem 41
File metadata and controls
37 lines (33 loc) · 787 Bytes
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
/*
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?
*/
#include <iostream>
#include<math.h>
#include<vector>
#include <algorithm>
#include<set>
using namespace std;
bool is_prime(int num){
if (num % 2 == 0){
return false;
}
for(int i = 3; i <= ceil(sqrt(num)); i+= 2){
if(num % i == 0){
return false;
}
}
return true;
}
int main() {
vector<int> primes;
string str = "1234567";
while( std::next_permutation(str.begin() , str.end()) ){
int convert = stoi(str);
if(is_prime(convert)){
primes.push_back(convert);
}
}
cout << primes[primes.size() - 1];
return 0;
}