-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path27-quadraticPrimes.py
89 lines (83 loc) · 1.72 KB
/
27-quadraticPrimes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import math
def is_prime(n):
if n < 2:
return False
n_sqrt = int(math.sqrt(n))+1
i = 2
while i < n_sqrt:
if n%i == 0:
return False
i += 1
return True
max_primes = 0
max_product = 0
for a in range(-1000,1001):
for b in range(-1000,1001):
primes = 0
for n in range (0,80):
p = n**2 + a*n + b
if is_prime(p):
primes += 1
if primes > max_primes:
max_primes = primes
max_product = a*b
print 'New max! ',a,b,a*b,primes
#elif primes > 0:
# break
print max_product
##http://projecteuler.net/problem=27
##f(n) = n^2 + an + b
##
##f(0) = b => b - prime
##f(1) = a+b+1 => a - odd
# from funkcje import isprime
# import time
# start = time.time()
#
# b_candidates = set(k for k in range(2,1000) if isprime(k))
# a_candidates = set(k for k in range(-999,1000,2))
#
#
# def consecutive_primes_fn(a, b):
# '''(int, int) -> int
#
# Return number of consecutive primes generated by f(n) with
# parameteres a, b.
#
# >>> consecutive_primes_fn(1,41)
# 40
# >>> consecutive_primes_fn(-79, 1601)
# 80
# '''
# result = 0
# k = 0
# helper = k**2 + a*k + b
# while helper > 0 and isprime(helper):
# result += 1
# k += 1
# helper = k**2 + a*k + b
# return result
#
#
# def euler27():
# max_n = 0
# product = 0
# for a in a_candidates:
# for b in b_candidates:
# temp = consecutive_primes_fn(a,b)
# if temp > max_n:
# max_n = temp
# product = a*b
# return product
#
#
# if __name__ == '__main__':
# import doctest
# doctest.testmod()
#
#
# print euler27()
# end = time.time()
# print end-start
##result: -59231
##time elapsed: 4.31599998474