zoukankan      html  css  js  c++  java
  • POJ 2262

    Description

    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 + 520 = 3 + 1742 = 5 + 37

    题意:任意一个数字都可以写成两质数之和,输入一个数字,列出其与两质数之和的等式。

    考查素数筛的使用,珍爱生命,学会打表。

     1 #include <string>
     2 #include <iostream>
     3 #include <map>
     4 #include <cstring>
     5 #include <algorithm>
     6 #include <math.h>
     7 #include <stdio.h>
     8 #define N 1000001
     9 using namespace std;
    10 
    11 int main()
    12 {
    13     bool a[N];
    14     int n;
    15     a[0]=a[1]=1;
    16     for(int i=2;i<N;i++)
    17     {
    18             if(!a[i])
    19             for(int j=2*i;j<N;j+=i)
    20             {
    21                 a[j]=1;
    22             }
    23     }
    24     while(scanf("%d",&n)&&n)
    25     {
    26         for(int i=0;i<=n;i++)
    27         {
    28             if(!a[i]&&!a[n-i])
    29             {
    30                 printf("%d = %d + %d
    ",n,i,n-i);
    31                 break;
    32             }
    33         }
    34     }
    35     return 0;
    36 }
  • 相关阅读:
    高精度加法和减法。加法还好,减法花了不少时间。
    整数拆分
    二叉搜索树的中位数
    基本有序数组的排序
    log4j手册
    mysql技巧
    vim配置文件
    regex for python like preg_match of php
    字符串按word反转
    the little redis 阅读笔记
  • 原文地址:https://www.cnblogs.com/greenaway07/p/10420018.html
Copyright © 2011-2022 走看看