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
#include
#include
#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;
}

Prime Factory (Training, Math)_第1张图片

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

10000331000037

即为所求。

你可能感兴趣的:(C)