SDUT 2809 Goldbach

来源:互联网 发布:mac 百度云下载太慢 编辑:程序博客网 时间:2024/04/30 11:39

Goldbach

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

The Goldbach’s Conjecture is stated like the following:
Every even number greater than 4 can be written as the sum of two odd prime numbers.
Your task is now to verify Goldbach\'s Conjecture for all even numbers less than a million.

输入

The input contains multiple test cases. Each test case consisted of an even integer n with 6 < n < 1000000. Input will be terminated by 0.

输出

For every test case, print one line of the form “n = a + b”, a < b, without quotes. Where a and b are odd prime numbers. If there is more than one pair of odd primes, you are to print the pair a, b with the maximized difference b - a. If there are no such pair, print a line saying “Goldbach\'s conjecture is wrong.”.

示例输入

8200

示例输出

8 = 3 + 520 = 3 + 17

#include <bits/stdc++.h>using namespace std;bool prim(int n){    for(int i=2;i*i<=n;i++)    {        if(n%i == 0)            return false;    }    return true;}int main(){    int n;    while(cin>>n && n)    {        for(int i=2;i<=n/2;i++)        {            if(prim(i))            {                int t = n - i;                if(prim(t))                {                    cout<<n<<" = "<<i<<" + "<<t<<endl;                    break;                }            }        }    }    return 0;






0 0
原创粉丝点击