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
def p44(): u = Utils() limit = 3000 pl = [f.p(i) for i in range(limit)] for a in range(1, limit): for b in range(1, limit): if a > b: c = pl[a] + pl[b] d = pl[a] - pl[b] if u.chop(c, pl) > -1 and u.chop(d, pl) > -1: print(a, b, pl[a], pl[b], pl[pl.index(c)], pl.index(c), pl[pl.index(d)], pl.index(d))
def test_is_prime_corner_cases(): u = Utils() actual_prime = u.is_prime(2) assert(actual_prime == True) actual_prime = u.is_prime(3) assert(actual_prime == True) actual_prime = u.is_prime(4) assert(actual_prime == False) actual_prime = u.is_prime(5) assert(actual_prime == True)
def test_is_prime(): u = Utils() actual_prime = u.is_prime(31) assert(actual_prime == True)
def test_to_matrix(): u = Utils() u.to_matrix('./test_matrix.in')
def all_in(l1, l2, u): u = Utils() for n in l1: if u.chop(n, l2) == -1: return False return True
# a palindrome. In fact, 10677 is the first number to be shown # to require over fifty iterations before producing a # palindrome: 4668731596684224866951378664 (53 iterations, # 28-digits). # # Surprisingly, there are palindromic numbers that are # themselves Lychrel numbers; the first example is 4994. # # How many Lychrel numbers are there below ten-thousand? # # NOTE: Wording was modified slightly on 24 April 2007 to # emphasise the theoretical nature of Lychrel numbers. from euler.utils import Utils u = Utils() def is_lychrel(n): iterations = 0 while iterations < 50: n += int(str(n)[::-1]) iterations += 1 if u.is_palindrome(str(n)): return False return True def p55(): total = 0 for n in range(1, 10000):
def test_is_composite(): u = Utils() actual_prime = u.is_prime(2200) assert(actual_prime == False)
#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())
# 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
# probability 2/3 or 'N' (NOT PRIME) with probability 1/3 just before jumping to # the next square. # When he is on a square with a number on it that is not a prime he croaks 'P' # with probability 1/3 or 'N' with probability 2/3 just before jumping to the next # 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():
# Clearly no BOPs exist for k ≥ 4. # # By considering the sum of FITs generated by the BOPs # (indicated in red above), we obtain 1 + 15 + 58 = 74. # # Consider the following tenth degree polynomial generating # function: # # u_n = 1 − n + n^2 − n^3 + n^4 − n^5 + n^6 − n^7 + n^8 − n^9 # + n^10. # Find the sum of FITs for the BOPs. import euler.numbers.functions as f from euler.utils import Utils u_ = Utils() m, val = u_.binom(11,11) def u(n): return n ** 10 - n ** 9 + n ** 8 - \ n ** 7 + n ** 6 - n ** 5 + \ n ** 4 - n ** 3 + n ** 2 - \ n + 1 U = [u(j) for j in range(1, 12)] def fit(n): l = [((-1) ** (p + (n % 2))) * m[n][p - 1] * U[p - 1] for p in range(1, n + 1)] return sum(l)
def test_sieve_set(): u = Utils() primes = u.sieve_set(50) assert(primes == set([2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]))
# positive integer, n, such that the expression n^3 + n^2p is # 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
# probability 2/3 or 'N' (NOT PRIME) with probability 1/3 just before jumping to # the next square. # When he is on a square with a number on it that is not a prime he croaks 'P' # with probability 1/3 or 'N' with probability 2/3 just before jumping to the next # 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):
def test_to_matrix_2(): u = Utils() u.to_matrix('./test_matrix_2.in', separator=',')
# # For n = 4 there are exactly three distinct solutions: # # \frac{1}{5} + \frac{1}{20} = \frac{1}{4} # \frac{1}{4} + \frac{1}{6} = \frac{1}{4} # \frac{1}{8} + \frac{1}{8} = \frac{1}{4} # # What is the least value of n for which the number of # distinct solutions exceeds one-thousand? # # NOTE: This problem is an easier version of Problem 110; it # 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():
#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_log_factorial(): u = Utils() actual = u.log_factorial(5) assert(actual > 0)
# 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)
def test_log_nPr(): u = Utils() actual = u.log_nPr(5, 4) assert(actual > 0)
# 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
# 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