zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    CCF-CSP201703-3 Markdown
    HDU1008 Elevator
    Java使用Preconditions.checkNotNull(.....)优雅地判空对象, 并处理可能的NullPointerException异常
    StringUtils的isBlank()方法
    java.math.RoundingMode 几个参数详解
    把String集合数据去"[ ]" 转成list和把String集合数据去除[ "" ,""] 转成list 工具类
    BeanUtils.copyProperties(复制对象属性方法)
    关于 mybatis 中 in 写法,<foreach collection="xxx" item="xxx" index="index" open="(" separator="," close=")"> 参数详解
    JAVA_collection_集合相关知识点(二、获取集合对象中某个属性的集合——CollectionUtils.collect()方法)
    @AllArgsConstructor @NoArgsConstructor
  • 原文地址:https://www.cnblogs.com/WangAoBo/p/6367360.html
Copyright © 2011-2022 走看看