projecteuler---->problem=27----Quadratic primes

Euler discovered the remarkable quadratic formula:

n2 + n + 41

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

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

Considering quadratics of the form:

n2 + an + b, where |a| < 1000 and |b| < 1000

where |n| is the modulus/absolute value of n

e.g. |11| = 11 and |?4| = 4

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

import math,time
def isOk(temp):
	if temp < 0: return False
	for i in range(2,int(math.sqrt(temp))+1):
		if temp%i==0 :
 			return False
	return True

begin = time.time()
maxValue=0
lastValue=1
for a in range(-1000,1001):
	for b in range(-1000, 1001):
		n = 0
		resu = 0
		while True:
			temp = n*n + a*n + b
			if(isOk(temp)):
				resu+=1
				n+=1
			else :
				break
		if resu>maxValue:
			maxValue = resu
			lastValue = a*b
print lastValue
end = time.time()
print end-begin

projecteuler---->problem=27----Quadratic primes

时间: 2024-09-29 09:14:55

projecteuler---->problem=27----Quadratic primes的相关文章

Project Euler:Problem 27 Quadratic primes

Euler discovered the remarkable quadratic formula: n2 + n + 41 It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n

Project-Euler problem 1-50

最近闲的做了下Project Euler 上的题目,前面50题都比较简单,简单总结下.代码一般是Python和C/C++的 用Python 做这些题目简直是酸爽啊 一下代码可能不一定是我的,因为不知道论坛里面的回复不是永久的,所以我的代码有的丢了,可能找个和我的意思相近的代码.题目翻译是从 欧拉计划 | Project Euler 中文翻译站上面Copy 的表告我. Problem 1  Multiples of 3 and 5 10以下的自然数中,属于3和5的倍数的有3,5,6和9,它们之和是

欧拉计划(python) problem 27

Quadratic primes Problem 27 Euler discovered the remarkable quadratic formula: n2 + n + 41 It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible

Project Euler:Problem 58 Spiral primes

Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed. 37 36 35 34 33 32 31 38 17 16 15 14 13 30 39 18  5  4  3 12 29 40 19  6  1  2 11 28 41 20  7  8  9 10 27 42 21 22 23 24 25 26 43 44 45 46

projecteuler----&amp;gt;problem=10----Summation of primes

title: The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. 翻译: 10下面的质数的和为2 + 3 + 5 + 7 = 17. 请求出200,0000下面全部质数的和. import math,time def isOk(a): for i in range(2,int(math.sqrt(a))+1): if a%i==0: retu

Project Euler:Problem 37 Truncatable primes

The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Fi

Project Euler:Problem 47 Distinct primes factors

The first two consecutive numbers to have two distinct prime factors are: 14 = 2 × 7 15 = 3 × 5 The first three consecutive numbers to have three distinct prime factors are: 644 = 22 × 7 × 23 645 = 3 × 5 × 43 646 = 2 × 17 × 19. Find the first four co

Codeforces Round #589 (Div. 2) (e、f没写)

https://codeforces.com/contest/1228/problem/A A. Distinct Digits 超级简单嘻嘻,给你一个l和r然后寻找一个数,这个数要满足的条件是它的每一位的数字不相同,找出满足要求的最小的那个数输出,没有找到就输出-1: 1 #include<bits/stdc++.h> 2 using namespace std; 3 bool check(int n){ 4 int vis[15]={0}; 5 while(n){ 6 if(!vis[n%

反素数 -- 数学

反素数就是区间内约数个数最多的那个数. 在ACM题目里, 一般是求约数最多而且数字最小的那个数,[1--n] 二是求约数刚好等于n的最小的那个数 三是求区间里的最小反素数[beign,end] 1和3有区别吗?有,1可以加速,3只能暴力 先说下思路 思路 : 官方题解 : (1)此题最容易想到的是穷举,但是肯定超时. (2)我们可以知道,计算约数的个数和质因数分解有着很大的联系: 若Q的质因数分解为:Q=p1^k1*p2^k2*…*pm^km(p1…pm为素数,k1…km≥1),则Q有(k1+1

第10章例题(紫书)

21/21 题目都很基础,有很多题书上讲得比较详细,然后隔得时间有点久,所以具体什么trick都忘了,思路也懒得去回忆,所以将就着放上来了.... 例题10–1 Uva 11582 题意:输入a, b, n让你计算F[a^b]%n;其中这个F[i]是斐波那契数: 题解: 这题是快速幂+找循环节,用什么方法找循环节呢?因为第一个数是0和1,然后当再出现0和1的时候就是出现循环节的时候,然后假如找到了循环节T,然后就有F[n] = F[n % T],预处理找循环节,O(一百万左右),快速幂logn