WeChall writeup :Prime Factory

题目: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.大于1000000

2.各位数字合也是质数。如23是质数,2+3=5也是质数。

写一段C程序,solution:10000331000037

    #include <stdio.h>  
    #include <stdlib.h>  
    #include <string.h>  
    #include <math.h>    
	
	
	int isprime1(int num)
	{
		for(int m=2;m<=sqrt(num);m++)
		{
			if(num%m==0)
				return 0; //num不是质数
		}	return 1;//num是质数
	}
	
	int isprime2(int num)
	{
		int sum=0;
		while(num)
		{
			sum+=num%10;
			num/=10;
		}
		if(isprime1(sum))
			return 1; //各位数字之和仍为质数
		else
			return 0;
	}
      
    int main()  
    {  
        int i;  
        char temp[100]= "";  
        FILE*fp=fopen("prime1.txt","w");  
                   
        for(i=3;i<1000100;i+=2)  
        {  
			if(i>1000000)
			{
            if(isprime1(i))
				if(isprime2(i))
				{
					sprintf(temp, "%d\n", i);		
					fwrite(temp,1,strlen(temp),fp); 
				}
			}				
        }  
        fclose(fp);  
        printf("DONE!!\n");  
        return 0;  
    }  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值