Prime Factory

扫码查看

Your task is simple:
Find the first two primes above 1 million, whose separate digit sums are also prime.
As example take 23, which is a prime whose digit sum, 5, is also prime.
The solution is the concatination of the two numbers,
Example: If the first number is 1,234,567
and the second is 8,765,432,
your solution is 12345678765432

你的任务是简单的:

找到第一个和第二个大于1百万,并且每个位上的数字的和都是素数的素数。

举例说明,素数23每个位上的数字之和为5,5同样也是个素数。

提交的答案是这两个数字联在一起的。

举例:如果第一个数字是

第二个数字是

你提交的答案应该是

#include <stdio.h>
#include <math.h> //判断整数x是否为素数
int isPrime( int x )
{
int flag=;
if(x==)
flag=;
for(int i=;i<= sqrt(x);i++)
{
if(x % i==)
{
flag=;
break;
}
}
return flag;
} //返回参数x,每个数位上的数字之和
int digitSum( int x )
{
int sum=;
while(x)
{
sum += x % ;
x /= ;
}
return sum;
} int main( void )
{
int n=;
int i=;
while(n<)
{
if( isPrime(i) && isPrime(digitSum(i)))
{
printf("%d",i);
n++;
}
i++;
}
}
05-07 11:49
查看更多