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


你可能感兴趣的:(wechall)