Example #1
0
def squares():
  for x in euler.integers(1):
    yield x**2
Example #2
0
 
"""
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x,
contain the same digits.
"""

# very easy once we have a neat way to check the digits of two numbers

from lib import euler


def have_same_digits(a, b):
  a_, b_ = str(a), str(b)
  if len(a_) != len(b_): return False
  a_map, b_map = {}, {}
  for n1, n2 in zip(a_, b_):
    a_map[n1] = 1 + a_map.get(n1, 0)
    b_map[n2] = 1 + b_map.get(n2, 0)
  return a_map == b_map


for x in euler.integers(2):
  same = True
  for m in xrange(2, 6):
    if not have_same_digits(x, x*m):
      same = False
      break
  if not same: continue
  print x
  break
    
Example #3
0
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
  if i in primes: continue
  found = False
  for p in primes.keys():
    diff = i - p
    if diff > 0:
      diff/=2
Example #4
0
"""
How many n-digit positive integers exist which are also an nth power?
"""

# easy to calculate, but I'm not sure what the upper bound is so this will
# just run until it's interrupted.
# clearly though, the exponential will grow a lot faster than the number of
# digits so I guess you could come up with some way of figuring out the
# upper bound

from lib import euler

integers = []
for power in euler.integers(1):
    for base in euler.integers(1):
        x = base ** power
        if len(str(x)) == power:
            integers += [x]
            print len(list(set(integers)))
        elif len(str(x)) > power:
            break