欧拉计划Problem 51

By replacing the 1st digit of *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.

By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.

Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.


#define N 1000000


bool isPrime(int x)
{
for (int n = sqrt(x),i = 2; i <= n ;i++)
{
if (x%i == 0)
{
return false;
}
}
return true;
}




int addivite(int p)
{
char c[10]={0};
int max = 0,num = 1,count = 0,ans;
sprintf(c,"%d",p);
for (int i = 0; i < strlen(c)-1; i++)
{
char temp = c[i];
num = 1,count = 1;
for (int j = i+1; j < strlen(c); j++)
{
num *= 10;
if (temp == c[j])
{
num++;
count++;
}
}
if (max < count)
{
max = count;
ans = num;
}
}
return ans;
}
int main()
{
clock_t ts,te;
ts=clock();
int answer = 0;
for (int i = 100000; i < N; i++)
{
for (int j = 1,count = 1,a = addivite(i); isPrime(i) == true && j < 10 ; j++)
{
if (isPrime(i+a*j) == true)
{
count++;
}
}
if (count == 8)
{
answer = i;
break;
}
}
printf("\nanswer %d",answer);
te=clock();
printf("\ntime difference: %ds\n",(te-ts)/CLOCKS_PER_SEC);
getchar();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值