欧拉计划Problem 49

The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.

There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.

What 12-digit number do you form by concatenating the three terms in this sequence?


#define N 10
#define M 10000
bool isPrime(int x)
{
for (int i = 2; i*i <= x; i++)
{
if (x%i == 0)
{
return false;
}
}
return true;
}


bool check(int c1,int c2,int c3)
{
int n1[N]={0},n2[N]={0},n3[N]={0};
for (int i = 0; i < 4; i++)
{
n1[c1%10]++;
n2[c2%10]++;
n3[c3%10]++;
c1 /= 10;
c2 /= 10;
c3 /= 10;
}
for (i = 0; i < N; i++)
{
if (n1[i] != n2[i] || n1[i] != n3[i] || n2[i] != n3[i])
{
return false;
}
}
return true;
}


int main()
{
clock_t ts,te;
ts=clock();
int answer = 0;
bool b = true;
int i,j;
for (i = 1488; i < M-6660 ;b = true,i++)
{
for (j = i; j <= i+6660; j+=3330)
{
if (isPrime(j) == false)
{
b =false;
break;
}
}
if (b == true)
{
if (check(i,i+3330,i+6660) == true)
{
answer = i;
break;
}
}
}
printf("\nanswer %d%d%d",answer,answer+3330,answer+6660);
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、付费专栏及课程。

余额充值