22 Mart 2013 Cuma
Thirty seventh solution of Project Euler
#include "stdafx.h"
#include <iostream>
#include<stdio.h>
#include<conio.h>
#include<math.h>
using namespace std;
bool check(int x[],int y);
bool prime(int y);
int create(int y);
bool prime(int y) {
if(y==1 || y == 0 ) {
return false;
}else if(y==2) {
return true;
}
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;
}
int create(int y) {
int tot=1;
for(int i=0;i<y;i++) {
tot*=10;
}
return tot;
}
bool check(int x[],int y) {
int tot=0,c=0;
for(int i=0;i<y;i++) {
tot=0;
for(int k=0;k<=i;k++) {
tot+=(x[k])*(create(k));
}
if(prime(tot)==false) {
c++;
return false;
}
}
y--;
if(c==0) {
for(int i=0;i<=y;i++) {
tot=0;
int h=i;
for(int k=0;k<=i;k++,h--) {
tot+=(x[y-k])*(create(h));
}
if(prime(tot)==false) {
c++;
return false;
}
}
}
if(c==0) {
return true;
}
}
void make() {
int c,k,ch=0,result=0;
for(unsigned int i=11;i<1000000;i++) {
int * dizi = new int[10];
k=0;
c=i;
do {
dizi[k]=c%10;
c/=10;
k++;
}while(c>0);
if(prime(i)==true) {
if(check(dizi,k)==true) {
ch++;
result+=i;
cout << i << ",";
if(ch==11) {
cout << "\nProject Euler - Problem 37\n";
cout << "Result :" << result << " efekanpulatli.blogspot.com" << endl;
break;
}
}
}
}
}
int main() {
make();
system("pause");
return 0;
}
Kaydol:
Kayıt Yorumları (Atom)

Hiç yorum yok:
Yorum Gönder