We Chall-Prime Factory-Writeup

MarkdownPad Document

We Chall-Prime Factory-Writeup

题目链接:http://www.wechall.net/challenge/training/prime_factory/index.php

网站打开较慢,所以把题目放在下方:

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

大意就是找到两个数,这两个数符合:是大于1000000的质数,并且数字的每一位加起来仍是质数,如:23是质数,2+3=5仍是质数;

因为没有时间的限制,所以用C++写了一个暴力循环的代码:

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 typedef long long LL;
 5 const LL MILLION = 1000000;
 6 
 7 bool IsPrime_1(LL num)
 8 {
 9     for(int i = 2; i <= sqrt(num); i++)
10         if(!(num % i))
11             return 0;//num不是质数
12 
13     return 1;//num是质数
14 }
15 
16 bool IsPrime_2(LL num)
17 {
18     LL sum = 0;
19     while(num)
20     {
21         sum += num % 10;
22         num /= 10;
23     }
24 
25     if(IsPrime_1(sum))
26         return 1;//各位数字的和仍为质数
27     else
28         return 0;
29 }
30 
31 int main()
32 {
33 //    LL num;
34 //    cin>>num;
35 //    if(IsPrime_1(num))
36 //        cout<<1<<endl;
37 //    if(IsPrime_2(num))
38 //        cout<<2<<endl;
39     for(LL i = MILLION+1 , cnt = 0 ; cnt < 2 ; i++)
40     {
41         if(IsPrime_1(i))
42             if(IsPrime_2(i))
43             {
44                 cnt++;
45                 cout<<i<<endl;
46             }
47     }
48 
49     return 0;
50 }

 

运行结果如下:

根据要求的形式得flag为:10000331000037

 

 

 

 

 

 

 

 

2017-2-5 12:40;11

posted @ 2017-02-05 12:38  M4x  阅读(896)  评论(0编辑  收藏  举报