Prime Factory (Training, Math)
题目描述
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
您的任务很简单:
找出100万以上的前两个素数,它们各自的位数和也是素数。
例如,以23为例,它是一个素数,其数字和5也是素数。
解决方案是将两个数字连接起来,
示例:如果第一个数字是1,234,567。
第二个是8,765,432,
你的解决方案是12345678765432
解:
如题编程写出算法找到符合条件的两个素数即可
C语言:
#include<stdio.h>
#include<math.h>
int isprime(long n)
{
int i,m;
m=sqrt(n);
for(i=2;i<=m;i++)
if(n%i==0) return 0;
return 1;
}
int main()
{
long num=1000000,prime1,prime2,sum=0,m;
int flag=0;
prime1=num;
while(true)
{
prime1++;
if(isprime(prime1)==1)
{
m=prime1;
while(m)
{
sum+=m%10;
m/=10;
}
if(isprime(sum)==1)
{
printf("%d",prime1);
sum=0;
break;
}
else
sum=0;
}
}
prime2=prime1;
while(true)
{
prime2++;
if(isprime(prime2)==1)
{
m=prime2;
while(m)
{
sum+=m%10;
m/=10;
}
if(isprime(sum)==1)
{
printf("%d",prime2);
break;
}
else
sum=0;
}
}
return 0;
}
没有编程基础的看这里:
得到关键信息。。。
wechall相关链接:我的wechall之旅??!