Ejemplo n.º 1
0
def euler10(target=2000000):
    """http://projecteuler.net/problem=10

    The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
    Find the sum of all the primes below two million.
    """
    return sum(sieve(target))
Ejemplo n.º 2
0
def main():
    primeArray = sieve(1000)
    limit = len(primeArray)
    n = 100
    # n = 10000  # possible improvement, as we search closer to the answer
    noDivsC1 = 2  # number of divisors of the "n" component
    d = 0
    while d < 500:
        n += 1
        aux = n
        noDivsC2 = 1  # number of divisors of the "n+1" component
        if aux % 2 == 0:
            aux /= 2
        for i in range(0, limit):
            if primeArray[i] * primeArray[i] > aux:
                noDivsC2 *= 2
                break
            exponent = 1
            while aux % primeArray[i] == 0:
                exponent += 1
                aux = aux / primeArray[i]
            if exponent > 1:
                noDivsC2 *= exponent
            if aux == 1:
                break
        d = noDivsC1 * noDivsC2
        # the number of divisors of the "n+1" component can be reused in the
        # next step
        noDivsC1 = noDivsC2
    print n * (n - 1) / 2  # the actual number, remember Problem 6?
    return 0
Ejemplo n.º 3
0
def euler35(upper_bound=1000000):
    """http://projecteuler.net/index.php?section=problems&id=35

    Find the sum of all numbers less than one million, which are palindromic in
    base 10 and base 2."""
    global primes
    primes = set(sieve(upper_bound))
    return sum(cyclic_prime(x) for x in primes)
Ejemplo n.º 4
0
def euler37(upper_bound=1000000):
    """http://projecteuler.net/index.php?section=problems&id=37
    
    Find the sum of all eleven primes that are both truncatable from left to
    right and right to left."""
    global primes
    primes = set(sieve(upper_bound))
    return sum(n for n in primes if is_truncatable(n))
Ejemplo n.º 5
0
def euler187(ub=10 ** 8):
    """http://projecteuler.net/problem=187

    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 < 108, have precisely two, not necessarily
    distinct, prime factors?
    """
    small_primes = sieve(int(ub ** 0.5))
    big_primes = sieve(ub / 2)
    total = 0
    for x in range(len(small_primes)):
        total += bisect(big_primes, ub / small_primes[x]) - x
    return total
Ejemplo n.º 6
0
def count_combinations(limit):
    primes = sieve(int(limit ** 0.5))
    squares = [p ** 2 for p in primes if p ** 2 < limit]
    cubes = [p ** 3 for p in primes if p ** 3 < limit]
    fourths = [p ** 4 for p in primes if p ** 4 < limit]
    results = set()
    for s in squares:
        for c in cubes:
            for f in fourths:
                if s + c + f < limit:
                    results.add(s + c + f)
    return len(results)
Ejemplo n.º 7
0
def euler7(n=10001):
    """http://projecteuler.net/problem=7

    By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see
    that the 6th prime is 13.

    What is the 10001st prime number?
    """
    # Find all primes less than some initial guess. If that doesn't provide
    # enough primes, try again with a bigger guess.
    primes, guess = [], 1000000
    while len(primes) < n:
        primes, guess = sieve(guess), guess * 2
    return primes[n - 1]
Ejemplo n.º 8
0
def main(nr):
    N = 1
    check = True
    limit = sqrt(nr)
    p = sieve(nr)
    a = [1] * nr
    for i in range(len(p)):
        if check:
            if p[i] <= limit:
                a[i] = floor(log(nr) / log(p[i]))
            else:
                check = False
        N *= p[i] ** a[i]
    print int(N)
    return 0
Ejemplo n.º 9
0
def euler46():
    """http://projecteuler.net/index.php?section=problems&id=46
    
    What is the smallest odd composite that cannot be written as the sum of a
    prime and twice a square?"""
    double_squares = [2*n*n for n in range(1000)]
    primes = set(sieve(10 ** 6))
    for n in range(2, 10 ** 6):
        found = False
        for ds in double_squares:
            if ds > n: break
            if n-ds in primes:
                found = True
                break
        if not found and n % 2 == 1: return n
Ejemplo n.º 10
0
def euler49():
    """http://projecteuler.net/index.php?section=problems&id=49

    Find arithmetic sequences, made of prime terms, whose four digits are
    permutations of each other."""
    primes = [prime for prime in sieve(10000) if prime > 999]
    prime_digits = [list(str(prime)) for prime in primes]
    for prime in prime_digits:
        prime.sort()
    prime_digits = [''.join(prime) for prime in prime_digits]
    for i in range(len(primes)):
        for j in range(i+1, len(primes)):
            if prime_digits[i] != prime_digits[j]: continue
            if 2 * primes[j] - primes[i] not in primes: continue
            x = 2 * primes[j] - primes[i]
            k = list(str(x))
            k.sort()
            k = ''.join(k)
            if (k == prime_digits[i]):
                if primes[i] != 1487:
                    return str(primes[i]) + str(primes[j]) + str(x)
Ejemplo n.º 11
0
def euler60():
    """http://projecteuler.net/problem=60

    The primes 3, 7, 109, and 673, are quite remarkable. By taking any two
    primes and concatenating them in any order the result will always be prime.
    For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of
    these four primes, 792, represents the lowest sum for a set of four primes
    with this property.

    Find the lowest sum for a set of five primes for which any two primes
    concatenate to produce another prime.
    """
    primes = sieve(10000)  # turns out 10k is enough for a set of 5.
    for p1 in primes:
        for p2 in primes:
            if p2 <= p1 or not concat_prime(p1, p2): continue
            for p3 in primes:
                if p3 <= p2 or not concat_primes(p3, [p1, p2]): continue
                for p4 in primes:
                    if p4 <= p3 or not concat_primes(p4, [p1, p2, p3]): continue
                    for p5 in primes:
                        if p5 <= p4 or not concat_primes(p5, [p1, p2, p3, p4]): continue
                        return sum([p1, p2, p3, p4, p5])
Ejemplo n.º 12
0
from euler_util import sieve
from itertools import combinations

primes = set(p for p in sieve(10 ** 6) if p > 10 ** 5)
templates = list(combinations(range(6), 3))


def fill_template(base, template, number):
    base = list(base)
    for index in template:
        base[index] = number
    return int("".join(base))


def test_templates(base, target=8):
    for template in templates:
        prime_substitutions = 0
        for i in range(10):
            if fill_template(str(base), template, str(i)) in primes:
                prime_substitutions += 1
        if prime_substitutions >= target:
            return fill_template(str(base), template, "1")
    return False


def euler51():
    """http://projecteuler.net/problem=51

    By replacing the 1st digit of *3, it turns out that six of the nine
    possible values: 13, 23, 43, 53, 73, and 83, are all prime.
Ejemplo n.º 13
0
def main(*args, **kwargs):
    print reduce(add, sieve(2000000))
    return 0
Ejemplo n.º 14
0
from euler_util import sieve

primes = set(sieve(10 ** 6))

def quad_score(a, b):
    score = 0
    while score*score + a*score + b in primes: score += 1
    return score

def euler27():
    """http://projecteuler.net/index.php?section=problems&id=27
    
    Find a quadratic formula that produces the maximum number of primes for
    consecutive values of n."""
    best_a, best_b, best_score = 0, 0, 0
    for a in range(-999, 1000):
        for b in range(-999, 1000):
            score = quad_score(a, b)
            if score > best_score:
                best_a, best_b, best_score = a, b, score
    return best_a * best_b
Ejemplo n.º 15
0
# coding=utf8
from euler_util import sieve

primes = sieve(1000)
def euler69(upper_bound=1000000):
    """http://projecteuler.net/problem=69

    Find the value of n <= 1,000,000 for which n/φ(n) is a maximum."""
    # Note: here is the brute force approach:
    # return max(zip(map(lambda x: float(x)/totient(x),
    #           xrange(1, ub)), xrange(1, ub)))
    # to do this more cleverly, realize that we minimize phi(n) when n is a
    # product of the first n primes.
    value = 1
    for p in primes:
        value *= p
        if value > upper_bound: return value/p