Advertisement
danchaofan

Euler #50

Dec 6th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. def prime(n):
  2.     if n == 2 or n == 3: return True
  3.     if n < 2 or n % 2 == 0: return False
  4.     if n < 9: return True
  5.     if n % 3 == 0: return False
  6.     r = int(n**0.5)
  7.     f = 5
  8.     while f <= r:
  9.         if n % f == 0: return False
  10.         if n % (f+2) == 0: return False
  11.         f += 6
  12.     return True
  13.  
  14. total, chain, bestchain, index, primes, bestprime = 0, 0, 0, 0, [], 0
  15. for x in range(10**6):
  16.     print(x)
  17.     if prime(x):
  18.         primes.append(x)
  19.         if (total + x) >= 10**6:
  20.             if (total + x) >= 10 ** 6:
  21.                 total -= primes[index]
  22.                 index += 1
  23.                 chain -= 1
  24.                 continue
  25.         total += x
  26.         chain += 1
  27.     if prime(total) and (total < 10**6):
  28.         if chain > bestchain:
  29.             bestchain = chain
  30.             bestprime = total
  31. print(bestprime)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement