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: }
  • 相关阅读:
    Cannot retrieve repository metadata (repomd.xml) for repository: fedora. P、、、
    Fedora 学习总结
    Gnome 高级配置
    [置顶]Fedora 13,14,15安装LAMP
    如何删去Fedora下应用程序菜单中的多余程序启动器?
    运用Telnet在windowsxp中与虚拟机中fedora14互联
    运用Telnet在Windows xp中与虚拟机中fedora14互连
    netstat 的应用
    在 fedora14 下升级 firefox 到 firefox4 的两种方案
    windows 中相关CMD应用
  • 原文地址:https://www.cnblogs.com/malloc/p/2494116.html
Copyright © 2011-2022 走看看