码迷,mamicode.com
首页 > 其他好文 > 详细

We Chall-Prime Factory-Writeup

时间:2017-02-05 13:46:42      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:dig   names   using   php   sep   tom   group   back   时间   

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

令:因ABO暂不熟悉flag,所以用了较繁琐的C++,同时因为没有限制时间,所以写了暴力循环的代码,以后熟悉python之后,会更新更为简洁的python代码;

 

 

 

 

 

2017-2-5 12:40;11

We Chall-Prime Factory-Writeup

标签:dig   names   using   php   sep   tom   group   back   时间   

原文地址:http://www.cnblogs.com/WangAoBo/p/6367360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!