コード例 #1
0
ファイル: 46.py プロジェクト: markwatkinson/project-euler
from sys import exit

"""
What we're going to do here is iterate over odd integers indefinitely,
filling up an array of primes and squares up to the current integer as we go.
Then we're going to iterate over the primes, subtracting the prime from the
integer, and seeing if the difference can be made up in the list of squares

HURRAH FOR GENERATORS
"""

def squares():
  for x in euler.integers(1):
    yield x**2

prime_generator = euler.primes()
square_generator = squares()

"""Using dicts for speedy lookup"""
primes = {}
squares = {}
for i in euler.integers(9, 2):
  
  while 1:
    p = prime_generator.next()
    primes[p] = True
    if p > i: break
  while 1:
    s = square_generator.next()
    squares[s] = True
    if s > i: break
コード例 #2
0
import atexit
from lib import euler

existing_primes = []
try:
  with open('data/primes') as f:
    l = f.read()
    existing_primes = [int(p) for p in l.splitlines()]
except IOError: pass

def write():
  global new_primes
  if new_primes:
    to_write = '\n'.join([str(n) for n in new_primes]) + '\n'
    with open('data/primes', 'a') as f:
      f.write(to_write)
  new_primes = []

i = len(existing_primes)
generator = euler.primes(i, existing_primes)
new_primes = []
while 1:
  new_primes += [generator.next()]
  if len(new_primes) > 1000: write()


コード例 #3
0
ファイル: 37.py プロジェクト: markwatkinson/project-euler
"""
Find the sum of the only eleven primes that are both truncatable from
left to right and right to left.

"""

# we don't know the upper bound, but since there's only 11 of them we don't
# need to, I guess.

from lib import euler

truncatable = []
primes = {} # we'll want to look up numbers we've already generated for primality
for p in euler.primes():
  primes[p] = True
  if p < 10: continue

  # truncate
  p_ltr = str(p)
  p_rtl = str(p)
  t = True
  while p_ltr:
    if not primes.get(int(p_ltr), False) or not primes.get(int(p_rtl), False):
      t = False
      break
    p_ltr = p_ltr[1:]
    p_rtl = p_rtl[:-1]
  if t:
    truncatable += [p]
    if len(truncatable) == 11:
コード例 #4
0
ファイル: 10.py プロジェクト: markwatkinson/project-euler
"""Find the sum of all the primes below two million."""

from lib import euler

primes = euler.primes()
s = 0
for p in primes:
  if p >= 2*10**6: break
  s += p
print s
コード例 #5
0
ファイル: 60.py プロジェクト: markwatkinson/project-euler

def concat(a, b):
  return int(str(a) + str(b))

def are_prime(l):
  for n in l:
    is_p = primes_map.get(n, None)
    if is_p is None:
      is_p = euler.is_prime(n)
      primes_map[n] = is_p
    if not is_p: return False
  return True


for i, p1 in enumerate(euler.primes()):
  #print p1
  primes += [p1]
  primes_map[p1] = True

  for j, p2 in enumerate(primes):
    c = [concat(p1, p2),
        concat(p2, p1)]
    if not are_prime(c): continue
    for k, p3 in enumerate(primes[j+1:]):
      c = [
        concat(p1, p3),
        concat(p3, p1),
        concat(p2, p3),
        concat(p3, p2),
      ]
コード例 #6
0
ファイル: 50.py プロジェクト: markwatkinson/project-euler
"""
Which prime, below one-million, can be written as the sum of the most
consecutive primes?
"""

# seems easy: generate list of primes up to 1m and have a look

from lib import euler


generator = euler.primes()
primes = [] # list of primes, as an optimisation, this only goes up to
#the last two primes p1 and p2 where p1 + p2 < 1m
primes_map = {} # lookup, this goes up to 1m
last_p = 0
for p in euler.primes():
  if p > 10**6: break
  if p + last_p < 10**6:
    primes += [p]
  last_p = p
    
  primes_map[p] = True

print 'generated primes', len(primes)

max_ = (0, 0)



for i, p in enumerate(primes):
  #print p