POJ 2262 Goldbach ' s conjecture (general sieve and linear sieve for solving prime numbers)

Source: Internet
Author: User


Goldbach ' s conjecture
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 40944 Accepted: 15664

Description

In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the foll Owing conjecture:
Every even number greater than 4 can be
Written as the sum of the 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 are right. (Oh wait, I had the proof of course, but it was too long to write it on the "this page.")
Anyway, your task is now-Verify Goldbach ' s conjecture for all even numbers less than a million.

Input

The input would contain one or more test cases.
Each test case consists of one even an integer n with 6 <= N < 1000000.
Input would 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 is odd primes. Numbers and operators should is separated by exactly one blank like in the sample output below. If there is more than a pair of odd primes adding up to N, choose the pair where the difference b-a is maximized. If There is no such pair, the print a line saying "Goldbach ' s conjecture is wrong."

Sample Input

820420

Sample Output

8 = 3 + 520 = 3 + 1742 = 5 + 37

Source

ULM Local 1998

Title Link: http://poj.org/problem?id=2262

The main idea: to divide a number into two odd prime numbers and to ask for the b-a difference

Title Analysis: The problem is water problem, from small to large enumeration can be, mainly by this problem to explain the solution of the prime number of two sieve method

Ordinary prime sieve time complexity O (NLOGN)
void Get_prime ()  {      memset (prime, true, sizeof (prime));      PRIME[1] = 0;      for (int i = 2; I * I <= max, i++)            if (Prime[i]) for                (int j = i * i; j <= MAX; j + = i)                    prime[j] = 0;  }


Time complexity O (n) of linear sieve
void Get_prime () {    pnum = 0;    Memset (Prime, true, sizeof (prime));    Prime[0] = false;    PRIME[1] = false;    for (int i = 2; i < MAX; i++)    {        if (Prime[i])            P[pnum + +] = i;        for (int j = 0; J < pnum && I * p[j] < MAX; j + +)        {            prime[i * P[j]] = false;            if (i% p[j] = = 0) break;}}}    

Ordinary sieve there is nothing to say, mainly linear sieve break there, such as 12 of this number, in the ordinary sieve 12 to be 2 and 3 are screened once, obviously this extra operation will increase the time complexity, the linear sieve in a number is only the smallest factor of the element sieve off, such as 12 by 2 sieve off, when I equals 6 6==12 Sieve off 12, this time 6%2==0 can break, if not broken, then 6 will also 18 sieve off, at this time is through the 6*3 to sift out 18, but obviously 18 the smallest factor is 2, so when I enumerated to 9 when there is 9*2==18, so 18 was sieved again, Therefore, when I equals 6, do not take 6 to sieve 18, the following formula to illustrate:
When Prime[j] is the factor of I, set i=prime[j]*k, because the element factor from small to large enumeration, so prime[j] is the minimum factor of I, at this time I have no need to remove Prime[j ']*i (j ' >j) Form of composite, because Prime[j '] *i can be written as Prime[j ']* (prime[j]*k) =prime[j]* (prime[j ']*k), meaning that all prime[j ']*i will be removed from the future of some I ' =prime[j ']*k, the current I is not required. For example, an example previously cited

The code:
#include <cstdio> #include <cstring>int const MAX = 1000005;int P[max];bool prime[max];int pnum;void get_    Prime () {pnum = 0;    Memset (Prime, true, sizeof (prime));    Prime[0] = false;    PRIME[1] = false;        for (int i = 2; i < MAX; i++) {if (Prime[i]) P[pnum + +] = i;            for (int j = 0; J < pnum && I * p[j] < MAX; J + +) {Prime[i * P[j]] = false;        if (i% p[j] = = 0) break;  }}}//void Get_prime ()//{//Memset (prime, true, sizeof);  PRIME[1] = 0;  for (int i = 2; I * i <= max; i++)//if (Prime[i])//(Int j = i * i; j <= Max; j  + = i)//prime[j] = 0;    }int Main () {get_prime ();    int n; while (scanf ("%d", &n)! = EOF && N) {for (int i = 3; I <= n/2; i + = 2) {if (pr Ime[i] && Prime[n-i]) {printf ("%d =%d +%d\n", N, I, n-i);            Break }        }       }}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2262 Goldbach ' s conjecture (general sieve and linear sieve for solving prime numbers)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.