Advertisement
Guest User

Untitled

a guest
May 20th, 2012
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. primes = tuple(n for n in range(1, 10**6) if is_prime(n)==True)
  2.  
  3. count_best = 0
  4.  
  5.        
  6. ##http://docs.python.org/release/2.3.5/lib/itertools-example.html:
  7. ## Slightly modified (first for loop)
  8. from itertools import islice
  9. def window(seq):
  10.     for n in range(2, len(seq) + 1):
  11.                  
  12.         it = iter(seq)
  13.         result = tuple(islice(it, n))
  14.         if len(result) == n:
  15.             yield result    
  16.         for elem in it:
  17.             result = result[1:] + (elem,)
  18.             yield result  
  19.  
  20. def memoize(function):
  21.     cache = {}
  22.     def decorated_function(*args):
  23.         if args in cache:
  24.             return cache[args]
  25.         else:
  26.             val = function(*args)
  27.             cache[args] = val
  28.             return val
  29.     return decorated_function
  30.  
  31.  
  32. @memoize
  33.  
  34.  
  35. def find_lin_comb(prime):
  36.     global count_best
  37.  
  38.     for windows in window(primes[0 : primes.index(prime)]):
  39.         if sum(windows) == prime and len(windows) > count_best:
  40.             count_best = len(windows)
  41.             print('Prime: ', prime, 'Terms: ', count_best)
  42.  
  43.  
  44. ##Find them:
  45. for x in primes[::1]: find_lin_comb(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement