04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 """ ns = [[int(i) for i in line.split()] for line in grid.split("\n") if line] def number_tuples(ns, size): maxrow = len(ns) maxcol = len(ns[0]) # for each starting corner for row in xrange(maxrow): for col in xrange(maxcol): if col <= maxcol - size: # - yield ns[row][col : (col + size)] if row <= maxrow - size: # | yield [nsr[col] for nsr in ns[row : (row + size)]] if row <= maxrow - size and col <= maxcol - size: # \ yield [ns[row + i][col + i] for i in xrange(size)] if row >= size - 1 and col <= maxcol - size: # / yield [ns[row - i][col + i] for i in xrange(size)] from pe5 import prod s = max(prod(nt) for nt in number_tuples(ns, 4)) assert s == 70600674
""" http://projecteuler.net/index.php?section=problems&id=9 A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. """ from pe5 import prod def find( k ) : # a + b > c; c = k - a - b # b > k/2 - a for a in xrange(1, k/3) : for b in xrange( k/2-a, k/2 ) : c = k - a - b if a**2 + b**2 == c**2 : return a,b,c assert prod(find(1000)) == 31875000
73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450 """.replace( "\n", "" ) from pe5 import prod ns = [int(i) for i in s] mp = max(prod(ns[i : i + 5]) for i in xrange(4, len(ns) - 5)) assert mp == 40824
def proper_divisors(factors) : multiplicants = [ [p**i for i in xrange(e+1)] for p,e in factors.items() ] orig_n = prod( m[-1] for m in multiplicants ) return filter( lambda pd : pd < orig_n, (prod(ns) for ns in product(*multiplicants)) )
def num_divisors2( factors ) : return prod( e+1 for e in factors.values() )
def num_divisors(n) : f = factorize(n) return prod( e+1 for e in f.values() )