Pku2262 Goldbach's Conjecture number theory-prime number

Source: Internet
Author: User

Pku2262 Goldbach's Conjecture

 

Prime [Max] In the number table of EPO with the sort method.

 

Previously, I used the simplest trial division method as the limit method, and thought it was very efficient. Then I kept using time limit exceed.

 

 

 

1,Trial Division

Divide N by 2-sqrt (N). If there is an integer that can be divided by X, it is not a prime number. Otherwise, it is a prime number.

Time Complexity: O (SQRT (n ))

 

2,Prime Number Embedding Method

This method is an improvement of the above method. The above method is to determine whether the number between 2-SQRT (n) can be exhausted. Because of the following arithmetic basic theorem, the judgment amount can be reduced.

Arithmetic basic theorem: Also known as the unique decomposition theorem of prime numbers, that is, each natural number greater than 1 can be written as the product of prime numbers, and after these prime factors are sorted by size, there is only one way to write.

For example, 6936 = 2 ^ 3 × 3 × 17 ^ 2, 1200 = 2 ^ 4 × 3 × 5 ^ 2. According to the basic arithmetic theorem, any sum can be divided into the product of some prime numbers, so we can determine whether a number can be divisible by the prime numbers between 2-SQRT (n.

However, you must know all prime numbers between 2-sqrt (n.

 

3,Limit Method

This method can find all prime numbers in a certain range.

The idea is to require all prime numbers within 10000 to list the numbers 1-10000. 1 is not a prime number, and 2 is a prime number. The multiples of all 2 are not prime numbers; take out the next surviving number and draw out all its multiples until all the multiples of the surviving number are broken.

To find all the prime numbers that 10000 thinks, you need an array with a size of 10000. Set all its elements to unmarked. First, set 1 to marked, starting from 2, mark all the numbers that are multiples of the number, and then mark the multiples of the number that is not marked next. After the mark is complete, all unlabeled numbers are prime numbers.

 

 

Question:

Each time you give an even number of records [6, 1000000)

 

Output: N = a + B

 

Requirements: A + B = N, A and B are odd prime numbers, and the value of B-A is the largest.

 

 

First obtain all prime numbers in 1000000, and then start from 3 to find a and B that meet the conditions.

 

Note that B = n-

Then B-A = N-2 *

So the larger the value of A, the smaller the value of B-.

Therefore, once a and B that meet the conditions are found, the break

Output A B

 

#include<stdio.h>bool prime[1000010];int main(){int i,j,n;for(i=3;i<=1000000;i+=2)prime[i]=1;for(i=3;i<500000;i++) for(j=1;j<1000000/i;j++)prime[i+j*i]=0;while(scanf("%d",&n)&&n!=0){for(i=3;i<=n/2;i+=2)if(prime[i]&&prime[n-i])break;printf("%d = %d + %d\n",n,i,n-i);}return 0;}

 

 

The following is an upgraded version ..

 

#include<stdio.h>bool prime[1000000];int main(){int i,j,n,i2,k;for(i=3;i<=1000000;i+=2)prime[i]=1;for(i=3;i<1000;i++) {if(prime[i]){i2=i+i;k=i*i;while(k<1000000){prime[k]=0;k+=i2;}} } while(scanf("%d",&n)&&n!=0){for(i=3;i<=n/2;i+=2)if(prime[i]&&prime[n-i])break;printf("%d = %d + %d\n",n,i,n-i);}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.