Euler program (python) problem 27

Source: Internet
Author: User

Quadratic Primesproblem 27

Euler discovered the remarkable quadratic formula:

N2 + n + 41

It turns out that the formula would produce the primes for the consecutive values n = 0 to 39. However, when n = 41, 402 + + + = (+ 1) +-is divisible by, and certainly when n = 412 + + clearly divisible by 41.

The incredible formula N2? 1601N + is discovered, which produces primes for the consecutive values n = 0 to 79. The product of the coefficients,?? and 1601, is? 126479.

Considering Quadratics of the form:

N2 + an + b, where | a| < | B| < 1000

where | N| is the Modulus/absolute value of n
e.g. |11| = One and |? 4| = 4

Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum Nu Mber of primes for consecutive values of N, starting with n = 0.


Python code:

Import Mathdef IsPrime (x): If X<3:return False for I in Range (2,int (math.sqrt (x)) +1): If x%i==0:        Return False return truedef func (A, B): K=0 while True:if IsPrime (k*k+a*k+b): k+=1 Else:break return k-1maxa,maxb=0,0num=0for J in Range ( -999,1000): If IsPrime (j): For I in RA Nge ( -999,1000): Temp=func (i,j) if Temp>num:num=temp maxa,maxb=i,jp Rint (MAXA*MAXB)

Time:3s

Euler program (python) problem 27

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.