zoukankan      html  css  js  c++  java
  • ZOJ Problem Set–1951 Goldbach's Conjecture

    Time Limit: 2 Seconds      Memory Limit: 65536 KB


    In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:

    Every even number greater than 4 can be
    written as the sum of two odd prime numbers.
    For example:
    8 = 3 + 5. Both 3 and 5 are odd prime numbers.
    20 = 3 + 17 = 7 + 13.
    42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.
    Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof of course, but it is too long to write it on the margin of this page.)
    Anyway, your task is now to verify Goldbach's conjecture for all even numbers less than a million.

    Input
    The input will contain one or more test cases.
    Each test case consists of one even integer n with 6 <= n < 1000000.
    Input will be terminated by a value of 0 for n.

    Output
    For each test case, print one line of the form n = a + b, where a and b are odd primes. Numbers and operators should be separated by exactly one blank like in the sample output below. If there is more than one pair of odd primes adding up to n, choose the pair where the difference b - a is maximized. If there is no such pair, print a line saying "Goldbach's conjecture is wrong."

    Sample Input
    8
    20
    42
    0

    Sample Output
    8 = 3 + 5
    20 = 3 + 17
    42 = 5 + 37


    Source: University of Ulm Local Contest 1998

      1:  #include<iostream>
    
      2:  #include<bitset>
    
      3:  #include<cmath>
    
      4:   
    
      5:  using namespace std;
    
      6:   
    
      7:  const int PRIME_NUM = 1000000; 
    
      8:   
    
      9:  bitset<PRIME_NUM> Primes;
    
     10:  void InitialPrimers()
    
     11:  {
    
     12:   Primes.set();
    
     13:   int i,k;
    
     14:   for(i=2;i<PRIME_NUM;i++)
    
     15:   {
    
     16:      k=2*i;
    
     17:      while(k<PRIME_NUM)
    
     18:      {
    
     19:        if(Primes.test(k))
    
     20:         Primes.reset(k);
    
     21:       k+=i;
    
     22:      }
    
     23:   }
    
     24:  }
    
     25:   
    
     26:  int main(void)
    
     27:  {
    
     28:    InitialPrimers();
    
     29:    int n;
    
     30: 
    
     31:    while(cin>>n && n)
    
     32:    {
    
     33:      bool find = false;
    
     34:      for(int i = 3;i <= n/2;i+=2)
    
     35:      {
    
     36:          if(Primes.test(n - i) && Primes.test(i))
    
     37:          {
    
     38:            cout<<n<<" = "<<i<<" + "<<n - i<<endl;
    
     39:            find = true;
    
     40:            break;
    
     41:          }
    
     42:      }
    
     43:      if(!find)
    
     44:        cout<<"Goldbach's conjecture is wrong."<<endl;
    
     45:    }
    
     46:   return 0;
    
     47: }
  • 相关阅读:
    spark学习(9)-spark的安装与简单使用
    Hadoop学习(8)-scala环境配置及简单使用
    Hadoop学习(7)-hive的安装和命令行使用和java操作
    Hadoop学习(5)-zookeeper的安装和命令行,java操作
    Hadoop学习(6)-HBASE的安装和命令行操作和java操作
    Hadoop学习(4)-mapreduce的一些注意事项
    Hadoop学习(3)-mapreduce快速入门加yarn的安装
    Hadoop学习(2)-java客户端操作hdfs及secondarynode作用
    python执行Linux命令
    python写的百度贴吧邮箱采集(带界面)
  • 原文地址:https://www.cnblogs.com/malloc/p/2494116.html
Copyright © 2011-2022 走看看