#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
unsigned long result;
bool prime(unsigned long y);
unsigned long getpow(int y);
bool prime(unsigned long y) {
if (y % 2 == 0)
return false;
for (long int i = 3; i <= (long int) sqrt((double) y); i++) {
if (y % i == 0)
return false;
}
return true;
}
unsigned long getpow(int y) {
unsigned long tot=1;
for(;y>0;y--) {
tot*=10;
}
return tot;
}
void make(int* iarr)
{
unsigned long tot=0;
tot=0;
for(int i=0; i<7; ++i) {
tot+=iarr[i]*getpow(6-i);
}
if(prime(tot)==1) { result=tot; }
}
int main()
{
clock_t tStart = clock();
int iarr[7] = {1,2,3,4,5,6,7};
sort(iarr,iarr+7);
make(iarr);
while(next_permutation(iarr,iarr+7))
make(iarr);
cout << "Project Euler - Problem 41(Pandigital prime)\n";
cout << "Result :" <<result << " efekanpulatli.blogspot.com" << endl;
printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
system("pause");
return 0;
}

Hiç yorum yok:
Yorum Gönder