Skip to content

Instantly share code, notes, and snippets.

@coderaven
Last active December 15, 2015 16:08
Show Gist options
  • Save coderaven/5286548 to your computer and use it in GitHub Desktop.
Save coderaven/5286548 to your computer and use it in GitHub Desktop.
Euler 41 in C++ Using Permutation
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
// Euler 41 in C++ Using Permutation - Solution by Raven G. Duran
// Common good ol' prime checker
bool is_prime(long int n){
int i;
if(n%2==0) return false;
int sRoot = sqrt((double)n);
for(i = 3; i <= sRoot; i+=2){
if(n%i==0) return false;
}
return true;
}
int main(){
int digits[] = {7,6,5,4,3,2,1};
long int n;
do {
std::stringstream s;
s << digits[0] << digits[1] << digits[2] << digits[3] << digits[4] << digits[5] << digits[6];
s >> n;
if (is_prime(n)){
std::cout << "The Highest Pandigital Prime Number is: " << n << std::endl;
break;
}
} while ( std::prev_permutation(digits,digits+7) );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment