def compute(): LIMIT = 10**7 smallestprimefactor = eulerlib.list_smallest_prime_factors(LIMIT) ans = 0 for i in range(1, LIMIT + 1): # Compute factorization as coprime prime powers. e.g. 360 = {2^3, 3^2, 5^1} factorization = [] j = i while j != 1: p = smallestprimefactor[j] q = 1 while True: j //= p q *= p if j % p != 0: break factorization.append(q) solns = [0] modulus = 1 for q in factorization: # Use Chinese remainder theorem; cache parts of it recip = eulerlib.reciprocal_mod(q % modulus, modulus) newmod = q * modulus solns = [((0 + (x ) * recip * q) % newmod) for x in solns] + \ [((1 + (x - 1) * recip * q) % newmod) for x in solns] modulus = newmod ans += max(solns) return str(ans)
def compute(): LIMIT = 10**7 smallestprimefactor = eulerlib.list_smallest_prime_factors(LIMIT) # Maximum size of set of prime factors where the product of the set <= LIMIT. # This is important because the number of solutions for n is 2^N, # where N is the number of distinct prime factors of n. maxnumprimefactors = 0 prod = 1 for i in range(2, len(smallestprimefactor)): if smallestprimefactor[i] == i: # i is prime if LIMIT // prod < i: break prod *= i maxnumprimefactors += 1 ans = 0 # Temporary arrays solns = [0] * (2**maxnumprimefactors) newsolns = [0] * (2**maxnumprimefactors) for i in range(1, LIMIT + 1): # Compute factorization as coprime prime powers. e.g. 360 = {2^3, 3^2, 5^1} factorization = [] j = i while j != 1: p = smallestprimefactor[j] q = 1 while True: j //= p q *= p if j % p != 0: break factorization.append(q) solns[0] = 0 solnslen = 1 modulus = 1 for q in factorization: # Use Chinese remainder theorem; cache parts of it recip = eulerlib.reciprocal_mod(q % modulus, modulus) newmod = q * modulus newsolnslen = 0 for j in range(solnslen): newsolns[newsolnslen] = (0 + ( (solns[j] - 0 + modulus) * recip % modulus) * q) % newmod newsolnslen += 1 newsolns[newsolnslen] = (1 + ( (solns[j] - 1 + modulus) * recip % modulus) * q) % newmod newsolnslen += 1 solnslen = newsolnslen modulus = newmod # Flip buffers solns, newsolns = newsolns, solns ans += max(solns[:solnslen]) return str(ans)
def compute(): LIMIT = 120000 smallestprimefactor = eulerlib.list_smallest_prime_factors(LIMIT - 1) rads = [0] for i in range(1, LIMIT): n = i rad = 1 while n > 1: p = smallestprimefactor[n] while True: n //= p if n % p != 0: break rad *= p rads.append(rad) # - gcd(a,b) = gcd(a,c) = gcd(b,c), so we only need to compute one of them. # - Since {a, b, c} are mutually coprime, rad(a * b * c) = rad(a) * rad(b) * rad(c). # - rad(a)*rad(b)*rad(c) < c implies rad(a)*rad(b)*rad(c) <= c-1 implies rad(a)*rad(b) <= floor((c-1)/rad(c)). sum = 0 for c in range(2, LIMIT): thres = (c - 1) // rads[c] for a in itertools.count(1): b = c - a if b <= a: break # The first two conditions are just optional optimizations if rads[a] <= thres and rads[b] <= thres and rads[a] * rads[ b] <= thres and fractions.gcd(a, b) == 1: sum += c return str(sum)
def compute(): LIMIT = 120000 smallestprimefactor = eulerlib.list_smallest_prime_factors(LIMIT - 1) rads = [0] for i in range(1, LIMIT): n = i rad = 1 while n > 1: p = smallestprimefactor[n] while True: n //= p if n % p != 0: break rad *= p rads.append(rad) # - gcd(a,b) = gcd(a,c) = gcd(b,c), so we only need to compute one of them. # - Since {a, b, c} are mutually coprime, rad(a * b * c) = rad(a) * rad(b) * rad(c). # - rad(a)*rad(b)*rad(c) < c implies rad(a)*rad(b)*rad(c) <= c-1 implies rad(a)*rad(b) <= floor((c-1)/rad(c)). sum = 0 for c in range(2, LIMIT): thres = (c - 1) // rads[c] for a in itertools.count(1): b = c - a if b <= a: break # The first two conditions are just optional optimizations if rads[a] <= thres and rads[b] <= thres and rads[a] * rads[b] <= thres and fractions.gcd(a, b) == 1: sum += c return str(sum)
def compute(): N = 20000000 K = 15000000 smallestprimefactor = eulerlib.list_smallest_prime_factors(N) def factorial_prime_factor_sum(n): result = 0 for i in range(n + 1): j = i while j > 1: p = smallestprimefactor[j] result += p j //= p return result ans = factorial_prime_factor_sum(N) - factorial_prime_factor_sum(K) - factorial_prime_factor_sum(N - K) return str(ans)