(Problem 49)Prime permutations

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?

题目大意:

1487, 4817, 8147这个序列,每个比前一个递增3330,而且这个序列有两个特点:1. 序列中的每个数都是质数。2. 每个四位数都是其他数字的一种排列。

1,2,3位组成的三个质数的序列中没有具有以上性质的。但是还有另外一个四位的递增序列满足这个性质。

如果将这另外一个序列的三个数连接起来,组成的12位数字是多少?

//(Problem 49)Prime permutations
// Completed on Thu, 13 Feb 2014, 15:35
// Language: C
//**********************************************
// 版权所有(C)acutus (mail: acutus@126.com)
// 博客地址:http://www.cnblogs.com/acutus///**********************************************
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<string.h>
int a[]; bool prim(int n)
{
int i;
for(i = ; i * i <= n; i++) {
if(n % i ==) return false;
}
return true;
} int cmp(const void *a, const void *b)
{
return (*(char*)a - *(char*)b);
} void init()
{
int i, j;
i = ;
j = ;
a[] = ;
while(j < ) {
if(prim(i)) {
a[j++] = i;
}
i += ;
}
} bool judge(int a, int b, int c)
{
char A[], B[], C[];
sprintf(A, "%d", a);
qsort(A, , sizeof(char), cmp);
sprintf(B, "%d", b);
qsort(B, , sizeof(char), cmp);
sprintf(C, "%d", c);
qsort(C, , sizeof(char), cmp);
if(strcmp(A, B)== && strcmp(A, C) == )
return true;
return false;
} void solve()
{
int i, b, c, d;
i = ;
init();
while(a[i++] < );
for(; i < ; i++) {
b = a[i]; c = a[i] + ; d = a[i] + ;
if(d < ) {
if(prim(b) && prim(c) && prim(d)) {
if(judge(b, c, d)) {
printf("%d %d %d\n", b, c, d);
}
}
}
} } int main()
{
solve();
return ;
}
Answer:
296962999629
上一篇:WebView js 调用Java本地方法


下一篇:linux进程——后台运行的方法