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 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 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)
# 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
def test_to_matrix_2(): u = Utils() u.to_matrix('./test_matrix_2.in', separator=',')
def test_is_composite(): u = Utils() actual_prime = u.is_prime(2200) assert(actual_prime == False)
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]))
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 test_log_nPr(): u = Utils() actual = u.log_nPr(5, 4) assert(actual > 0)
def test_log_factorial(): u = Utils() actual = u.log_factorial(5) assert(actual > 0)
def all_in(l1, l2, u): u = Utils() for n in l1: if u.chop(n, l2) == -1: return False return True