def problem131(): seen = 0 for m in count(2): factors = set(factorize(m)) n = m**3 for k in range(0,n,product(factors)**2): if any( k % p != 0 for p in factors): continue if k % product(factors) != 0: print(k) if (n**3 - k**3) % n**2 == 0 and isPrime((n**3 - k**3) // n**2): p = (n**3 - k**3) // n**2 if p > 10**6: return seen print(n, p, k) seen += 1
def problem231(): n = 20000000 c = 15000000 total = 1 for factor in genFactors(n): prod = product(factor) if n - c < prod <= n: total += sum(factor) if 1<= prod <= c: total -= sum(factor) return total
def problem108a(): recordN = 10**9 recordSolutions = 0 for n in genProducts(10**6,[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59]): n = product(n) if n > 10: m = basicSolutions(n) #if m >= 1000 and n < recordN: if m > recordSolutions: print(n,m) recordN = n recordSolutions = m
def problem127(): GOAL = 120000 rad = {} # rad[6] = {2,3}, radn[8] = {2} for primes in genFactors(GOAL): rad[product(primes)] = (set(primes), product(set(primes))) def relprime(s, t): return s & t == set() found = 0 total = 0 for b in range(1, GOAL): for a in range(1, min(b, GOAL - b)): c = a + b x, y, z = rad[a], rad[b], rad[c] if x[0] & y[0] != set(): continue if x[1] * y[1] * z[1] < c: found += 1 total += c return total
def problem108(): def update(minimum, n=1, divs=1, index=0, power=50): # Worst than what we found so far or used up all the primes if n >= minimum or index >= len(primes): return minimum # We found n with enough divisors if divs >= 2*(4*10**6): return n # the current prime we are looking at p = primes[index] # All the possible powers for e in range(1, power+1): minimum = min(minimum, update(minimum, n * p**e, divs * (2*e+1), index+1, e)) return minimum primes = primesUpTo(50) minimum = product(primes) return update(minimum)
def problem108(): for n in genProducts(10**6,[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59]): if numberOfSolutions(product(n)) > 10**3: return product(n)
def divisors(n): if type(n) == type(0): return {product(a) for a in powerset(factorize(n))} if type(n) in [type(()), type(set())]: return {product(a) for a in powerset(n)}
A little observation shows us that (5k)! ≡g 5!^k * k! So our function f(n) is defined recursively: f(0) = 1 f(n = 5k + j) = g((5k + 1)(5k + 2)...(5k + j) * 12k * f(k)) ''' def f(n): ''' Returns the last 5 trailing non-zero digits of n!''' if n == 0: return 1 r = product(5 * (n // 5) + i + 1 for i in range(n % 5)) r *= pow(12, n // 5, 10 ** 5) * f(n // 5) while r % 10 == 0: r //= 10 return r % 10 ** 5 def problem160(): return f(10 ** 12) from cProfile import run if __name__ == "__main__": # run("problem160()") print(problem160() == 16576)
def PSR(primes): return max([product(c) for c in powerset(primes) if product(c) < product(primes) ** (1 / 2)])