(title) Search for prime numbers

Source: Internet
Author: User

Looking for prime numbers to timelimit:1000ms memorylimit:128000kb 64-bit integer IO format:%lldProblem Description

Goldbach Guess we all know a little bit. We are not trying to prove this conclusion, but we want to take an even number out of the set of numbers that can be represented within the language of the program, to find two primes so that they are equal to that even.
By doing this, we can show that this conjecture is set up.
Since there can be different prime pairs to represent the same even number, it is specifically required that the prime pair being searched is the closest of two.

Input

The input is some even integer M (5<m<=10000).

Output

For each even number, output two prime numbers closest to each other, and equal to the even number.

Sampleinput
203040
Sampleoutput
7 1313 1717 23

1#include <stdio.h>2 intMain ()3 {4     intm,i,j;5      while(SCANF ("%d", &m)! =EOF)6     {7     intNum[m];8      for(i=0; i<m;++i)///initializing an array of tables9num[i]=1;Ten      for(i=2; i<m;++i)//Building a prime table One          for(j=2;; ++j) A     { -         if(i*j>m) -              Break; thenum[i*j]=0; -     } -      for(i=m/2;; I.)//traversing the number of primes -     { +  -         if(num[i]==1&& num[m-i]==1)//The condition to be satisfied by the prime number +              Break; A     } atprintf"%d%d\n", i,m-i); -     } -     return 0; -}
Ac Code

AC Ideas:
1. Use the input value m as the limit and enumerate the prime list. (Because of the nearest prime number of M to the vicinity of a certain M/2)
2. Take I=M/2 as the starting point, traverse forward, and then check if the m-i is a prime number when it encounters a prime number. This allows you to check the number of prime pairs that are nearest to each other and equal to M.

Another way of thinking:

Look at other people's code after AC, and find another way.

1. Still traverse forward or backward from M/2.

2. Write a function Isprimer (), check whether a value I is a prime number, the amount is, then check whether the corresponding M-i is a prime, if so the nearest prime is the two number.

Thinking: After reading it is not necessary to establish a prime number table, because it can only need to make two numbers to determine the prime or not. This reduces the computational effort.



(title) Search for 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.