Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- primes = tuple(n for n in range(1, 10**6) if is_prime(n)==True)
- count_best = 0
- ##http://docs.python.org/release/2.3.5/lib/itertools-example.html:
- ## Slightly modified (first for loop)
- from itertools import islice
- def window(seq):
- for n in range(2, len(seq) + 1):
- it = iter(seq)
- result = tuple(islice(it, n))
- if len(result) == n:
- yield result
- for elem in it:
- result = result[1:] + (elem,)
- yield result
- def memoize(function):
- cache = {}
- def decorated_function(*args):
- if args in cache:
- return cache[args]
- else:
- val = function(*args)
- cache[args] = val
- return val
- return decorated_function
- @memoize
- def find_lin_comb(prime):
- global count_best
- for windows in window(primes[0 : primes.index(prime)]):
- if sum(windows) == prime and len(windows) > count_best:
- count_best = len(windows)
- print('Prime: ', prime, 'Terms: ', count_best)
- ##Find them:
- for x in primes[::1]: find_lin_comb(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement