def euler50(): """ The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953. Which prime, below one-million, can be written as the sum of the most consecutive primes? """ primes = euler_util.gen_primes_to(1000000) longest = 0 sum_prime = 0 for start in range(0, len(primes)): if primes[start] * longest > 1000000: break for stop in range(start + longest + 1, len(primes)): cand = sum(primes[start:stop]) if cand > 1000000: break if cand in primes: length = stop - start + 1 if length > longest: (longest, sum_prime) = (length, cand) return sum_prime
def euler50(): """ The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953. Which prime, below one-million, can be written as the sum of the most consecutive primes? """ primes = euler_util.gen_primes_to(1000000) longest = 0 sum_prime = 0 for start in range(0, len(primes)): if primes[start] * longest > 1000000: break for stop in range(start+longest+1, len(primes)): cand = sum(primes[start:stop]) if cand > 1000000: break if cand in primes: length = stop - start + 1 if length > longest: (longest, sum_prime) = (length, cand) return sum_prime
def euler46(): """ It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2x1^2 15 = 7 + 2x2^2 21 = 3 + 2x3^2 25 = 7 + 2x3^2 27 = 19 + 2x2^2 33 = 31 + 2x1^2 It turns out that the conjecture was false. What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? """ primes = euler_util.gen_primes_to(10000) for i in range(3, 10000, 2): if i in primes: continue satisfied = False for root in range(1, int((i / 2)**0.5) + 1): if (i - 2 * root**2) in primes: satisfied = True break if not satisfied: return i
def euler35(): """ The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. How many circular primes are there below one million? """ primes = euler_util.gen_primes_to(1000000) circular = [] for prime in primes: scale = 10**(len(str(prime))-1) if skip(prime, scale) or prime in circular: continue next = ((prime % scale) * 10) + (prime / scale) tmp = [prime] is_circular = True while next != prime: if next not in primes: is_circular = False break else: tmp.append(next) next = ((next % scale) * 10) + (next / scale) if is_circular: circular += tmp return len(circular)
def euler87(): """ The smallest number expressable as the sum of a prime square, prime cube, and prime fourth power is 28. In fact, there are exactly four numbers bleow fifty that can be expressed in such a way. How many numbers below fifty million can be expressed as the sum of a prime square, a prime cube, and prime fourth power? """ primes = euler_util.gen_primes_to(int(50000000**0.5) + 1) nums = [] for sqr in range(len(primes)): square = primes[sqr]**2 cub = 0 while square + primes[cub]**3 < 50000000: cube = primes[cub]**3 quad = 0 while square + cube + primes[quad]**4 < 50000000: fourth = primes[quad]**4 nums.append(square + cube + fourth) quad += 1 cub += 1 return len(set(nums))
def euler87(): """ The smallest number expressable as the sum of a prime square, prime cube, and prime fourth power is 28. In fact, there are exactly four numbers bleow fifty that can be expressed in such a way. How many numbers below fifty million can be expressed as the sum of a prime square, a prime cube, and prime fourth power? """ primes = euler_util.gen_primes_to(int(50000000**0.5)+1) nums = [] for sqr in range(len(primes)): square = primes[sqr]**2 cub = 0 while square + primes[cub]**3 < 50000000: cube = primes[cub]**3 quad = 0 while square + cube + primes[quad]**4 < 50000000: fourth = primes[quad]**4 nums.append(square + cube + fourth) quad += 1 cub += 1 return len(set(nums))
def euler35(): """ The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. How many circular primes are there below one million? """ primes = euler_util.gen_primes_to(1000000) circular = [] for prime in primes: scale = 10**(len(str(prime)) - 1) if skip(prime, scale) or prime in circular: continue next = ((prime % scale) * 10) + (prime / scale) tmp = [prime] is_circular = True while next != prime: if next not in primes: is_circular = False break else: tmp.append(next) next = ((next % scale) * 10) + (next / scale) if is_circular: circular += tmp return len(circular)
def euler134(): acc = 0 primes = euler_util.gen_primes_to(1000004) primes = primes[primes.index(5):] for i, p1 in enumerate(primes[:-1]): acc += S(p1, primes[i + 1]) return acc
def gen_totients(n): ps = gen_primes_to(n) ts = range(n) for p in ps: for idx in range(p, n, p): ts[idx] *= (1.0 - 1.0 / p) return ts
def euler134(): acc = 0 primes = euler_util.gen_primes_to(1000004) primes = primes[primes.index(5):] for i, p1 in enumerate(primes[:-1]): acc += S(p1, primes[i+1]) return acc
def euler381(): ps = gen_primes_to(10**8) total = 0 for p in ps: if p > 5: total += S(p) elif p == 5: total += 4 return total
def euler123(): primes = gen_primes_to(10**6) for n, p in enumerate(primes): if (n + 1) % 2 == 0: continue rem = 2 * (n + 1) * p if rem > 10000000000: return n + 1 return -1
def euler187(): n = 10**8 ps = gen_primes_to(n / 2) count, i, j = 0, 0, len(ps)-1 while i <= j: while ps[i] * ps[j] >= n: j -= 1 count += j-i+1 i += 1 return count
def euler187(): n = 10**8 ps = gen_primes_to(n / 2) count, i, j = 0, 0, len(ps) - 1 while i <= j: while ps[i] * ps[j] >= n: j -= 1 count += j - i + 1 i += 1 return count
def gen_totients(n): totients = range(n+1) primes = euler_util.gen_primes_to(n+1) for p in primes: scale = 1 idx = p while idx <= n: totients[idx] *= (1.0 - 1.0 / p) scale += 1 idx = scale * p return totients
def gen_totients(n): totients = range(n + 1) primes = euler_util.gen_primes_to(n + 1) for p in primes: scale = 1 idx = p while idx <= n: totients[idx] *= (1.0 - 1.0 / p) scale += 1 idx = scale * p return totients
def euler03(): """ The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? """ n = 600851475143 ps = gen_primes_to(10000) for p in ps: if n % p == 0: n /= p if p > n: return p return -1 # failure
def starting_point(limit): primes = euler_util.gen_primes_to(100) idx = 0 top, bottom = 1, 1 # fast upper bound since phi(prime) = prime - 1 while float(top) / float(bottom) > limit: idx += 1 bottom = reduce(lambda x,y: x*y, primes[:idx]) - 1 top = reduce(lambda x,y: x*y, [p-1 for p in primes[:idx]]) # starting point is the product of all primes before the upper bound d = reduce(lambda x,y: x*y, primes[:idx-1]) phi_d = reduce(lambda x,y: x*y, [p-1 for p in primes[:idx-1]]) return d, phi_d
def euler27(): """ Euler discovered the remarkable quadratic formula: n^2 + n + 41 It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41^2 + 41 + 41 is clearly divisible by 41. The incredible formula n^2 - 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients, -79 and 1601, is -126479. Considering quadratics of the form: n^2 + an + b, where |a| < 1000 and |b| < 1000 where |n| is the modulus/absolute value of n e.g. |11| = 11 and |-4| = 4 Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0. """ primes = euler_util.gen_primes_to(1000) longest = 0 product = 0 idx = 0 while idx < len(primes): b = primes[idx] for a in range(-999, 1000): consec = 0 n = 0 quad_eq = lambda x: x**2 + a * x + b while quad_eq(n) in primes: consec += 1 n += 1 if consec > longest: (longest, product) = (consec, a * b) idx += 1 return product
def euler27(): """ Euler discovered the remarkable quadratic formula: n^2 + n + 41 It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41^2 + 41 + 41 is clearly divisible by 41. The incredible formula n^2 - 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients, -79 and 1601, is -126479. Considering quadratics of the form: n^2 + an + b, where |a| < 1000 and |b| < 1000 where |n| is the modulus/absolute value of n e.g. |11| = 11 and |-4| = 4 Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0. """ primes = euler_util.gen_primes_to(1000) longest = 0 product = 0 idx = 0 while idx < len(primes): b = primes[idx] for a in range(-999, 1000): consec = 0 n = 0 quad_eq = lambda x: x**2 + a*x + b while quad_eq(n) in primes: consec += 1 n += 1 if consec > longest: (longest, product) = (consec, a * b) idx += 1 return product
def euler203(): vals = [1, 2] row = [1, 2, 1] for _ in range(51-3): row = gen_next_row(row) vals += row vals = set(vals) ps = gen_primes_to(int(max(vals)**0.5)) sm = 0 for val in vals: if square_free(val, ps): sm += val return sm
def euler203(): vals = [1, 2] row = [1, 2, 1] for _ in range(51 - 3): row = gen_next_row(row) vals += row vals = set(vals) ps = gen_primes_to(int(max(vals)**0.5)) sm = 0 for val in vals: if square_free(val, ps): sm += val return sm
def euler512(): n = 5 * 10**8 ps = gen_primes_to(n+1) ts = range(n+1) s, last = 0, 3 for p in ps: for r in xrange(p, n, p): ts[r] = int(ts[r] * (1.0 - 1.0 / p)) for i in xrange(last, p, 2): s += ts[i] last = p for i in xrange(last, n+1, 2): s += ts[i] return s
def euler243(): # resilient fractions of n = numbers < n coprime to n = phi(n) (eulers totient) lower_limit = 15499.0 / 94744.0 d, phi_d = starting_point(lower_limit) # generate totients for composites primes = euler_util.gen_primes_to(100) phi = totients(primes) # multiply the starting point by increasing composites for i in range(4, 100): if i in primes: continue new_d = i * d new_phi_d = phi[i] * phi_d if new_phi_d / (new_d-1) < lower_limit: return new_d
def euler214(): n = 40000000 ps = gen_primes_to(n) ts = range(n) ls = [0] * n s = 0 # calculate totients for p in ps: for r in range(p, n, p): ts[r] = int(ts[r] * (1.0 - 1.0 / p)) ls[1] = 1 for i in range(2, len(ls)): ls[i] = ls[ts[i]] + 1 # if prime chain is 25 if ts[i] == i - 1 and ls[i] == 25: s += i return s
def euler214(): n = 40000000 ps = gen_primes_to(n) ts = range(n) ls = [0]*n s = 0 # calculate totients for p in ps: for r in range(p, n, p): ts[r] = int(ts[r] * (1.0 - 1.0 / p)) ls[1] = 1 for i in range(2, len(ls)): ls[i] = ls[ts[i]] + 1 # if prime chain is 25 if ts[i] == i-1 and ls[i] == 25: s += i return s
def euler70(): limit = (10**4) m, n = 1000.0, 0 ns = [] # looking for a composite number with 2 large prime factors ~sqrt(10^7) ps = euler_util.gen_primes_to(limit) fac1, fac2 = split_ps(ps) for f1 in fac1[::-1]: for f2 in fac2: c = f1 * f2 if c > 10**7: break t = (f1 - 1) * (f2 - 1) # fast totient if not perm(t, c): continue ratio = f1 * f2 / float(t) if ratio < m: m, n = ratio, f1 * f2 return n
def euler70(): limit = (10**4) m, n = 1000.0, 0 ns = [] # looking for a composite number with 2 large prime factors ~sqrt(10^7) ps = euler_util.gen_primes_to(limit) fac1, fac2 = split_ps(ps) for f1 in fac1[::-1]: for f2 in fac2: c = f1 * f2 if c > 10**7: break t = (f1-1)*(f2-1) # fast totient if not perm(t, c): continue ratio = f1*f2 / float(t) if ratio < m: m, n = ratio, f1*f2 return n
from euler_util import gen_primes_to b_ns = [0] c_ns = [0] primes = gen_primes_to(100) def prime_divisors(n): divisors = [] for p in primes: if p > n: break if n % p == 0: divisors.append(p) return divisors def prime_partitions(n): # this done via a euler transform c_n = sum(prime_divisors(n)) tmp = 0 for k in range(len(b_ns)): tmp += c_ns[k]*b_ns[n-2-k] b_n = (c_n + tmp) / n b_ns.append(b_n) c_ns.append(c_n) return b_n def euler77():
import euler_util primes = euler_util.gen_primes_to(9000) def is_prime(n): for p in primes: if p > n**0.5: return True if n % p == 0: return False return True def build_cands(p): cands = [] start = primes.index(p)+1 for i in range(start, len(primes)): p_ = primes[i] if is_prime(int(str(p)+str(p_))) and is_prime(int(str(p_)+str(p))): cands.append(p_) return cands def find_pairs(lists): target = 5 sets = [] # FUGLY CODE AHEAD for lst1 in lists: if len(lst1) < target: continue for el2 in lst1[1:]: lst2 = lists[primes.index(el2)] if len(lst2) < target - 1: continue
def euler204(): ps = gen_primes_to(typ) gen_hamming_num(ps, 1) return count
def euler110(): ps = gen_primes_to(50) gen_hcn(ps, 1, 8, 1) return lowest
def euler108(): ps = gen_primes_to(25) gen_hcn(ps, 1, 8, 1) return lowest
def prime_squares(n): ps = gen_primes_to(n) return [p*p for p in ps]
def prime_squares(n): ps = gen_primes_to(n) return [p * p for p in ps]
from euler_util import gen_primes_to b_ns = [0] c_ns = [0] primes = gen_primes_to(100) def prime_divisors(n): divisors = [] for p in primes: if p > n: break if n % p == 0: divisors.append(p) return divisors def prime_partitions(n): # this done via a euler transform c_n = sum(prime_divisors(n)) tmp = 0 for k in range(len(b_ns)): tmp += c_ns[k] * b_ns[n - 2 - k] b_n = (c_n + tmp) / n b_ns.append(b_n) c_ns.append(c_n) return b_n
import euler_util primes = euler_util.gen_primes_to(1000000) # fix bound later? def is_trunc(p): single_primes = [2,3,5,7] if not ((p % 10) in single_primes): return False if not ((p / 10 ** (len(str(p))-1)) in single_primes): return False left = p / 10 while left > 0: if not euler_util.is_prime(left): return False left /= 10 right = p % (10 ** (len(str(p))-1)) while right > 0: if not euler_util.is_prime(right): return False right = right % (10 ** (len(str(right))-1)) return True def euler37(): """ The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, 3. Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
import euler_util primes = euler_util.gen_primes_to(1000000) # fix bound later? def is_trunc(p): single_primes = [2, 3, 5, 7] if not ((p % 10) in single_primes): return False if not ((p / 10**(len(str(p)) - 1)) in single_primes): return False left = p / 10 while left > 0: if not euler_util.is_prime(left): return False left /= 10 right = p % (10**(len(str(p)) - 1)) while right > 0: if not euler_util.is_prime(right): return False right = right % (10**(len(str(right)) - 1)) return True def euler37(): """ The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, 3.