[Projecteuler.net] 27

Source: Internet
Author: User

The question website comes from the Chinese project Euler

Euler Once published a famous quadratic formula:

N2 +N+ 41

This formula can generate 40 prime numbers for consecutive numbers ranging from 0 to 39. However, whenNAt 40 o'clock, 402 + 40 + 41 = 40 (40 + 1) + 41 can be divisible by 41. WhenN41 at a time of 41, 41 + 41 + 41 can obviously be divisible by 41.

Using computers, people found an amazing formula:N2. 79N+ 1601. This formula is applicableN= 0 to 79 can generate 80 prime numbers. The coefficient of this formula, the product of 79 and 1601 is 126479.

Consider the quadratic formula in the following form:

N2 +An+B, Where |A| 1000 ,|B| 1000

Where | N| Indicates N.
For example, | 11 | = 11, | 4 | = 4

For continuousNGenerate the second formula of the maximum number of prime numbers to find the coefficient product of the formula.

**************************************** **************************************** ****

The optimization was not known at the first solution, and the computing took a lot of time.

The reason is that two layers-999 ~ 999 of the cycles, the cycle body needs to be calculated about 3.6 million times

In the Forum,Saif provides a filter for a B so that the cycle is about 1.6 million times.

N ^ 2 + An + B = n (n + a) + B = a prime number (we can see that B is a prime number; A is an odd number)

He said: because the result is a prime number, And when n = 0 is the expression equal to B, B must be a prime number (condition 1)

Analyze a now. When N is an odd number, only when a is an odd number is n (n + a) is an even number (the result may be a prime number)

When N is an even number, and only when a is an odd number, n (n + a) is an even number, and the result may be a prime number.

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.