POJ 2262 Goldbach & #39; s Conjecture (prime correlation), pojconjecture

Source: Internet
Author: User

POJ 2262 Goldbach's Conjecture (prime correlation), pojconjecture

POJ 2262 Goldbach's Conjecture (prime correlation)

Http://poj.org/problem? Id = 2262

Question:

To give you an even number in the range of [6,], you need to represent it in the form of two prime numbers plus each other. If multiple groups of solutions exist, output the solution with the greatest difference between two prime numbers.

Analysis:

First, we use the prime number embedding method to obtain all prime numbers within W.

The 'distinct' method is used to find the prime number:

Http://blog.csdn.net/u013480600/article/details/41120083

For a given number X, if there is a prime number a + prime number B = X, and the gap between a and B is the largest. Then we only need to enumerate prime number a from small to large, and then use X-a to be B. Then judge whether B is a prime number. (Think about it?)

AC code:

# Include <cstdio> # include <cstring> # include <algorithm> using namespace std; const int maxn = 1000000; // bool not_prime [maxn + 5] by prime number embedding; // not_prime [I] = true table I is not a prime number int prime [maxn + 5]; int get_prime () {memset (prime, 0, sizeof (prime )); // Note: When not_prime [I] = false, I is a prime memset (not_prime, 0, sizeof (not_prime); for (int I = 2; I <= maxn; I ++) {if (! Not_prime [I]) prime [++ prime [0] = I; for (int j = 1; j <= prime [0] & prime [j] <= maxn/I; j ++) {not_prime [I * prime [j] = true; if (I % prime [j] = 0) break;} return prime [0];} int main () {// generate all prime numbers within W get_prime (); int x; while (scanf ("% d", & x) = 1 & x) {int a, B; // x is split into two numbers: a and B. bool OK = false; for (int I = 1; I <= prime [0] & prime [I] <= x/2; I ++) {a = prime [I]; B = x-a; if (! Not_prime [B]) // if B is a prime number {OK = true; break ;}} if (OK) printf ("% d = % d + % d \ n ", x, a, B); else printf ("Goldbach's conjecture is wrong. \ n ") ;}return 0 ;}

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.