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

您的任务很简单:
求出一百万以上的前两个素数,其单独的数字总和也是素数。
以23为例,这是一个质数,其数字和5也是质数。
解决的办法是将两个数字叠加起来,
示例:如果第一个数字是1,234,567
第二个是8,765,432,
您的解决方案是12345678765432

/**********  a.c  ************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 10000000
char vis[N];//memset函数处理的是字符数组 
int all_sum(int aint){
	int re = 0;
	while(aint)
	{
		int re2 = aint % 10;
		re += re2;
		aint /= 10;
	}
	return re;
}
int main(){
	memset(vis,0,sizeof(vis)); 
	
	int m = sqrt(N + 0.5);
	memset(vis,0,sizeof(vis));
	for(int i=2;i<=m;i++){//初始化vis数组,相当于建表 
		for(int j=i*i;j<=N;j+=i){
			vis[j]=1;//删除(意味着把j从素数列表中排除) 
		}
	}
	//后续直接查表 
	int a,b;
	while(scanf("%d %d",&a,&b)!=EOF){
		int count=0;
		for(int i=a;i<=b;i++){
			if(vis[i] == 0 && vis[all_sum(i)] == 0){
				printf("%d ",i);
				
				count++;
				if(count%10==0){//一行最多打印10个数据 
					printf("\n");
				}
			}
		}
		if(count%10!=0){//为下一组数据的输出预留空间,且要防止在上一行中已经进行了换行。 
			printf("\n");
		}
	} 
	
	return 0;
}

然后把1000033和1000037进行拼接,得到

10000331000037

即为所求。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值