-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathsolution_1.py
More file actions
73 lines (54 loc) · 1.41 KB
/
solution_1.py
File metadata and controls
73 lines (54 loc) · 1.41 KB
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
#!/usr/bin/env python
# coding=utf-8
# Python Script
#
# Copyleft © Manoel Vilela
#
#
from functools import wraps
from itertools import combinations as comb
LIMIT = 1000
def memo(fn):
"""Memorization decorator"""
cache = {}
miss = object()
@wraps(fn)
def wrapper(*args, **kwargs):
result = cache.get(args, miss)
if result is miss:
result = fn(*args)
cache[args] = result
return result
return wrapper
def quadratic(a, b):
"""quadratic function abstraction"""
def func(n):
return n * n + a * n + b
return func
@memo
def isprime(n):
"""memorized prime evaluation, 2x more faster on that algorithm"""
for p in range(2, int(abs(n) ** 0.5) + 1):
if n % p == 0:
return False
return True
def eval_func(func):
"""eval func which primes will generate"""
n = 0
while True:
prime = isprime(func(n))
if prime:
n += 1
else:
return n
def search(limit):
"""search for the best a, b coefficients for a quadratic func prime gen"""
coffs = {}
for t in comb(range(-limit, limit + 1), 2):
coffs[t] = max([eval_func(quadratic(a, b)) for a, b in [t, t[::-1]]])
return max(coffs, key=lambda x: coffs[x])
def main():
from functools import reduce
print(reduce(int.__mul__, search(LIMIT)))
if __name__ == '__main__':
main()