def p46(): #getting precalculated primes and squares: u = Utils() primes = u.sieve(6000) squares = [i * i for i in range(1, 101)] #start searching at 15: o = 15 while o < 5800: #is the test number prime? if u.chop(o, primes) != -1: #yeap, go to next one: o += 2 continue #nope, begin testing: ind = 0 found = False while o - primes[ind] > 0: s = (o - primes[ind]) / 2 #found desired decomposition? if u.chop(s, squares) != -1: #yep, break out: found = True break #nope, try next prime: else: ind += 1 #found possible candidate: if not found: return o #keep searching: o += 2
def p231(): u = Utils() primes = u.sieve(20000000) total = 0 for p in primes: total += p * (f.v(20000000, p) - (f.v(5000000, p) + f.v(15000000, p))) return total
def p35(): u = Utils() sieve = u.sieve(10**6) count = 0 for prime in sieve: s = str(prime) l = cyclic_shifts_of(s) if all_in(l, sieve, u): count += 1 return count
# factors. For example, 15 = 3 × 5; 9 = 3 × 3; 12 = 2 × 2 # × 3. # # There are ten composites below thirty containing precisely # two, not necessarily distinct, prime factors: 4, 6, 9, 10, # 14, 15, 21, 22, 25, 26. # # How many composite integers, n < 10^8, have precisely two, # not necessarily distinct, prime factors? from euler.utils import Utils from math import sqrt from bisect import bisect u = Utils() #One of the accepted solutions from the forum (by #logopetria, @Sat, 22 Mar 2008, 08:45): primes = u.sieve(5 * (10**7)) def p187(): N = 10**8 total = 0 for x in range(bisect(primes, sqrt(N))): p = primes[x] total += bisect(primes, N / p) - x return total print(p187())
# Consider the consecutive primes p1 = 19 and p2 = 23. It can be verified that # 1219 is the smallest number such that the last digits are formed by p1 whilst # also being divisible by p2. # # In fact, with the exception of p1 = 3 and p2 = 5, for every pair of consecutive # primes, p2 > p1, there exist values of n for which the last digits are formed by # p1 and n is divisible by p2. Let S be the smallest of these values of n. # # Find S for every pair of consecutive primes with 5 <= p1 <= 1000000. from euler.utils import Utils u = Utils() a = u.sieve(1100000) def phi(n): """ It is assumed that n is a power of 10. """ return (2 * n) // 5 def f1(p1, p2): m = len(str(p1)) r = 10 ** m q = phi(r) - 1 s = pow(p2, q, r) return ((((p1 * p2 * s) // r) % p2) * r) + p1 def p134(): l = [f1(a[i], a[i + 1]) for i in range(2, len(a) - 1) if a[i] < 10 ** 6] return sum(l)
#coding: UTF-8 # A composite is a number containing at least two prime # factors. For example, 15 = 3 × 5; 9 = 3 × 3; 12 = 2 × 2 # × 3. # # There are ten composites below thirty containing precisely # two, not necessarily distinct, prime factors: 4, 6, 9, 10, # 14, 15, 21, 22, 25, 26. # # How many composite integers, n < 10^8, have precisely two, # not necessarily distinct, prime factors? from euler.utils import Utils from math import sqrt from bisect import bisect u = Utils() #One of the accepted solutions from the forum (by #logopetria, @Sat, 22 Mar 2008, 08:45): primes = u.sieve(5 * (10 ** 7)) def p187(): N=10**8 total=0 for x in range(bisect(primes, sqrt(N))): p = primes[x] total += bisect(primes, N/p) - x return total print(p187())
def test_sieve(): u = Utils() primes = u.sieve(50) assert(primes == [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47])
# is strongly advised that you solve this one first. from euler.utils import Utils u = Utils() MAX = 200000 def get_exp(n, p): i = 0 while n % p == 0: i += 1 n = n / p return i prime_list = u.sieve(MAX) # calculating the desired number using memoization, stopping # as soon as we find it: def p108(): i = 2 l = [0, 1] while i < MAX: for p in prime_list: if i % p == 0: val = l[i // p] + \ 2 * l[i // (p ** get_exp(i, p))] - 1 if val >= 1000: return i else: l.append(val)
# square. # # Given that the frog's starting position is random with the same probability for # every square, and given that she listens to his first 15 croaks, what is the # probability that she hears the sequence PPPPNNPPPNPPNPN? # # Give your answer as a fraction p/q in reduced form. from fractions import Fraction from euler.utils import Utils u = Utils() d = {} primes = u.sieve(500) for i in range(1, 501): if i in primes: d[("P", i)] = Fraction(2, 3) d[("N", i)] = Fraction(1, 3) else: d[("N", i)] = Fraction(2, 3) d[("P", i)] = Fraction(1, 3) def p(s, i): if (s, i) in d.keys(): return d[(s, i)] else: if i == 1:
# a perfect cube. # # For example, when p = 19, 8^3 + 8^2×19 = 12^3. # # What is perhaps most surprising is that for each prime with # this property the value of n is unique, and there are only # four such primes below one-hundred. # # How many primes below one million have this remarkable # property? from euler.utils import Utils cube_list = [x**3 for x in range(578)] u = Utils() prime_list = u.sieve(10**6) def p131(): """ As taken from the forum: Since x^3 = n^2(n + p), and p is a prime, it turns out that n must be a cube, as well as n + p, i.e., we must have p = a^3 - b^3 for some a, b. But, for a^3 - b^3 = (a - b)(a^2 + ab + b^2) to be prime we must have a - b = 1, so p must be a difference of consecutive cubes. """ total = 0 for i in range(len(cube_list) - 1):
#coding: UTF-8 # Let p_n be the nth prime: 2, 3, 5, 7, 11, ..., and let r # be the remainder when (p_n − 1)^n + (p_n + 1)^n is divided # by p_n^2. # # For example, when n = 3, p_3 = 5, and 4^3 + 6^3 = 280 # ≡ 5 mod 25. # # The least value of n for which the remainder first exceeds # 10^9 is 7037. # # Find the least value of n for which the remainder first # exceeds 10^10. from euler.utils import Utils u = Utils() p = u.sieve(4 * (10 ** 5)) def p123(): i = 0 a = 0 while a < 10 ** 10 and i < len(p): i += 1 a = (2 * p[i] * (i + 1)) % (p[i] ** 2) # sum 2 because i is the index of the number just below # 10 ** 10, and array indices start by 0: return i + 2 print(p123())
# be the remainder when (p_n − 1)^n + (p_n + 1)^n is divided # by p_n^2. # # For example, when n = 3, p_3 = 5, and 4^3 + 6^3 = 280 # ≡ 5 mod 25. # # The least value of n for which the remainder first exceeds # 10^9 is 7037. # # Find the least value of n for which the remainder first # exceeds 10^10. from euler.utils import Utils u = Utils() p = u.sieve(4 * (10**5)) def p123(): i = 0 a = 0 while a < 10**10 and i < len(p): i += 1 a = (2 * p[i] * (i + 1)) % (p[i]**2) # sum 2 because i is the index of the number just below # 10 ** 10, and array indices start by 0: return i + 2 print(p123())
# square. # # Given that the frog's starting position is random with the same probability for # every square, and given that she listens to his first 15 croaks, what is the # probability that she hears the sequence PPPPNNPPPNPPNPN? # # Give your answer as a fraction p/q in reduced form. from fractions import Fraction from euler.utils import Utils u = Utils() d = {} primes = u.sieve(500) for i in range(1, 501): if i in primes: d[("P", i)] = Fraction(2, 3) d[("N", i)] = Fraction(1, 3) else: d[("N", i)] = Fraction(2, 3) d[("P", i)] = Fraction(1, 3) def p(s, i): if (s, i) in d.keys(): return d[(s, i)] else: if i == 1: d[(s, 1)] = p(s[0], 1) * p(s[1:], 2)
# Consider the consecutive primes p1 = 19 and p2 = 23. It can be verified that # 1219 is the smallest number such that the last digits are formed by p1 whilst # also being divisible by p2. # # In fact, with the exception of p1 = 3 and p2 = 5, for every pair of consecutive # primes, p2 > p1, there exist values of n for which the last digits are formed by # p1 and n is divisible by p2. Let S be the smallest of these values of n. # # Find S for every pair of consecutive primes with 5 <= p1 <= 1000000. from euler.utils import Utils u = Utils() a = u.sieve(1100000) def phi(n): """ It is assumed that n is a power of 10. """ return (2 * n) // 5 def f1(p1, p2): m = len(str(p1)) r = 10**m q = phi(r) - 1 s = pow(p2, q, r) return ((((p1 * p2 * s) // r) % p2) * r) + p1