def findFactorsPairs(N): factors = [] bound = (N // 2) + 1 norig = N checkup = N / 5 for k in sieveEratosthenes(bound): j = 0 while N % k == 0: j += 1 N = N // k if j > 0: factors.append((k,j)) if N == 1: break if len(factors) == 0: factors.append(N) assert lu.product(factors) == norig, (str(factors) + (", %d != %d" % (lu.product(factors), norig))) return factors
def factorTrialDivision(N,primeList=None,verbose=False): factors = [] if primeList == None: bound = (N // 2) + 1 primeList = sieveEratosthenes(bound) norig = N # Original N for latter verification i = 0 checkup = N / 5 for k in primeList: while N % k == 0: factors.append(k) N = N // k if N == 1: break if verbose and i % (checkup) == 0: print "%d..." % k i = i+1 if len(factors) == 0: factors.append(N) assert lu.product(factors) == norig, (str(factors) + (", %d != %d" % (lu.product(factors), norig))) return factors
def numDivisors(N): factors = factorTrialDivision(N) return lu.product(lu.addOne(lu.listCount(factors)))
if prod > maxprod: maxprod = prod print maxprod ##################################################################### # Problem 12 if problem == 12: val = 0 for x in xrange(9,50000): # Find prime factors of n*(n-1)/2 numdiv = 0 if x % 2 == 0: factorx = pf.factorTrialDivision(x//2) factorx1 = pf.factorTrialDivision(x+1) factorx.extend(factorx1) numdiv = lu.product(lu.addOne(lu.listCount(factorx))) else: factorx = pf.factorTrialDivision(x) factorx1 = pf.factorTrialDivision((x+1)//2) factorx.extend(factorx1) numdiv = lu.product(lu.addOne(lu.listCount(factorx))) if (numdiv) >= 500: val = x break print nf.cumsum(val) ##################################################################### # Problem 13 # OK I cheated... thank you big number library of python if problem == 13: