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

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

 

 

 

 

 

2017-2-5 12:40;11

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值