zoukankan      html  css  js  c++  java
  • UVa 543 Godbach's Conjecture

      Goldbach's Conjecture 

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

    Every number greater than 2 can be written as the sum of three prime numbers.

    Goldbach cwas considering 1 as a primer number, a convention that is no longer followed. Later on, Euler re-expressed the conjecture as:

    Every even number greater than or equal to 4 can be expressed as the sum of two 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 as expressed by Euler for all even numbers less than a million.

    Input 

    The input file will contain one or more test cases.

    Each test case consists of one even integer n with $6 le 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
    

    Miguel A. Revilla 
    1999-01-11

    验证哥德巴赫猜想,给一个大于等于4的偶数,将它分解成两个素数之和。

    先求出0~1000000之间的所有素数,然后从小到大试验即可。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 
     7 bool isPrime[1000050];
     8 int prime[80000];
     9 
    10 int initial()
    11 {
    12     for(int i=1;i<=1000000;i++)
    13         isPrime[i]=true;
    14     isPrime[0]=isPrime[1]=false;
    15 
    16     for(int i=4;i<=1000000;i+=2)
    17         isPrime[i]=false;
    18     for(int i=3;i<=1000;i+=2)
    19         if(isPrime[i])
    20             for(int j=i*2;j<=1000000;j+=i)
    21                 isPrime[j]=false;
    22 
    23     int t=0;
    24     for(int i=1;i<=1000000;i++)
    25         if(isPrime[i])
    26             prime[t++]=i;
    27 
    28     return t;
    29 }
    30 
    31 int main()
    32 {
    33     int t=initial();
    34 
    35     int n;
    36 
    37     while(scanf("%d",&n)==1&&n)
    38     {
    39         int a=-1;
    40         for(int i=0;i<t;i++)
    41         {
    42             if(isPrime[n-prime[i]])
    43             {
    44                 a=prime[i];
    45                 break;
    46             }
    47         }
    48         if(a!=-1)
    49             printf("%d = %d + %d
    ",n,a,n-a);
    50         else
    51             puts("Goldbach's conjecture is wrong.");
    52     }
    53 
    54     return 0;
    55 }
    [C++]
  • 相关阅读:
    C# 中使用using的三种方法
    RabbitMQ系列文章
    c# 枚举(Enum)
    c# 占位符 {0} {1}
    C#中Lambda表达式总结
    c# 类型转换 int.TryParse() 方法,日期转换
    sql select 0 字段 某字段是不在指定的表,编一个字段
    Apache服务器安装配置(win版)
    安装 Apache 出现 <OS 10013> 以一种访问权限不允许的方式做了一个访问套接字的尝试
    windows安装Apache HTTP服务器报错:无法启动,因为应用程序的并行配置不正确
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3565852.html
Copyright © 2011-2022 走看看