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: }
  • 相关阅读:
    数据库访问性能优化--应用开发
    苹果终端wifi图标点亮慢和portal弹窗机制分析以及处理办法和建议
    蓝牙固件升级(OTA升级)原理设计
    C语言:内存字节对齐详解
    linux du与ls查看文件大小时的区别
    80211 组播速率及组播转单播
    视频帧、码流计算
    IEEE802.11数据帧在Linux上的抓取 80211格式转8023帧格式
    wifidog源码分析
    无线路由:关于WDS,Repeater等模式的说明
  • 原文地址:https://www.cnblogs.com/malloc/p/2494116.html
Copyright © 2011-2022 走看看