""" confusing spiral patterns, read the description on the site http://projecteuler.net/index.php?section=problems&id=28 """ # the key to this one is to generate the diagonals, not the whole # spiral. It's not that hard if you notice each additional layer adds a # range of numbers, and the diagonals are situated at the 1/4, 1/2, 3/4, 1 # parts of the range. from lib import spiraldiagonals diagonals = [1] last_number = 1 generator = spiraldiagonals.diagonals() for x in range(3, 1002, 2): ds = generator.next() diagonals += [d for d in ds] print sum(diagonals)
""" http://projecteuler.net/index.php?section=problems&id=58 """ from lib import spiraldiagonals from lib import euler """ this is pretty easy - we can generate the diagonals indefinitely from the formula we came up with in q28 and simply check the prime ratio it's a bit slow, probably the prime check, but still less than a minute """ primes = [] ds = [1] for i, diagonals in enumerate(spiraldiagonals.diagonals()): for d in diagonals: ds += [d] if euler.is_prime(d): primes += [d] ratio = float(len(primes)) / len(ds) if ratio <= 0.1: print (i+1)*2 + 1 # this is the spiral width break