Prime Factory (Training, Math)

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之旅??!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值