Beispiel #1
0
def eul7():
    """What is the 10001st prime number?"""
    i, primes = 1, eul.sieve(10)
    while len(primes) <= 10001:
        i += 1
        primes = eul.sieve(10 ** i)
    return primes[10000]
Beispiel #2
0
def eul37():
    """Find the sum of the only eleven primes that are both truncatable from left to right and right to left."""
    primes = [p for p in eul.sieve(1000000) if eul.odd(p)]
    truncs = {23}
    index = 4
    while index < len(primes):
        left = right = primes[index]
        prime = True
        while left > 0 and prime:
            if left not in primes:
                prime = False
            left = int(left / 10)

        if prime:
            iterator = 10
            while iterator < right:
                if right % iterator not in primes:
                    prime = False
                iterator *= 10

        if prime:
            truncs.add(primes[index])
        index += 1

    return sum(truncs)
Beispiel #3
0
def eul35():
    """How many circular primes are there below one million?"""

    def check_digits(m):
        while m > 0:
            digit = m % 10
            if digit == 5 or digit % 2 == 0:
                return False
            m = int(m / 10)
        return True

    primes = [p for p in eul.sieve(1000000) if check_digits(p)]

    circulars = {2, 5}
    for prime in primes:
        if prime not in circulars:
            perms = {prime}
            for i in range(0, len(str(prime)) - 1):
                prime = int(str(prime)[1:]) * 10 + int(str(prime)[0])
                perms.add(prime)
            circular = True
            for perm in perms:
                if perm not in primes:
                    circular = False
                    break
            if circular:
                circulars.update(perms)
    return len(circulars)
Beispiel #4
0
def eul60():
    primes = sieve(10000)
    sums = [999999999]

    for i in range(len(primes)):
        prime1 = primes[i]
        lists = []
        for j in range(i+1, len(primes)):
            prime2 = primes[j]

            if prime2 in checker[prime1] or check(prime1, prime2):
                checker[prime2].add(prime1)
                for k in range(len(lists)):
                    lst = lists[k]
                    if sum(lst)+prime2 > min(sums):
                        continue
                    if check_list(lst, checker[prime2], prime2):
                        checker[prime2] |= set(lst)
                        lst.append(prime2)
                lists.append([prime1, prime2])
                #print(lists)
        for lst in lists:
            if len(lst) == 5:
                sums.append(sum(lst))
        print(prime1, sums)

    return min(sums)
Beispiel #5
0
def eul49():
    """What 12-digit number do you form by concatenating the three n=4-digit terms in this sequence where
     (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another?"""
    primes = eul.sieve(10000)
    for prime in primes:
        if prime > 1000:
            perms = set([int(''.join(p)) for p in itertools.permutations(str(prime)) if int(''.join(p)) > 1000])
            inter = sorted(list(perms.intersection(primes)))
            if len(inter) >= 3:
                for combo in itertools.combinations(inter, 3):
                    if combo[0] != 1487 and combo[2]-combo[1] == combo[1]-combo[0]:
                        return ''.join(sorted([str(p) for p in combo]))
Beispiel #6
0
def eul47():
    """Find the first four consecutive integers to have four distinct prime factors.
    What is the first of these numbers?"""
    curr_n = 0
    primes = eul.sieve(200000)
    for n in range(20000, 200000):
        if eul.num_prime_factors(primes, n) != 4:
            curr_n = 0
        elif not curr_n:
            curr_n = n
        elif n - curr_n == 3:
            return curr_n
Beispiel #7
0
def eul5():
    """What is the smallest positive number that is evenly divisible by all of the numbers from 1 to n = 20?"""
    primer = functools.reduce(operator.mul, eul.sieve(20), 1)
    for i in range(primer, math.factorial(20) + 1, primer):
        divisible = True
        for factor in range(1, 20 + 1):
            if i % factor != 0:
                divisible = False
                break
        if divisible:
            return i
    return math.factorial(20)
Beispiel #8
0
def eul50():
    """Which prime, below n = one-million, can be written as the sum of the most consecutive primes?"""
    primes = eul.sieve(1000000)
    consecutive = 0
    maximum = 0

    for start in range(0, len(primes) - 2):
        r = 2 + consecutive
        for num in range(r, len(primes) - start):
            test = sum(primes[i] for i in range(start, start + num))
            if test > 1000000:
                break
            if test in primes and num > consecutive:
                consecutive = num
                maximum = test
    return maximum
Beispiel #9
0
def eul46():
    """What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?"""
    primes = eul.sieve(10000)
    squares = [x**2 for x in range(1, int(math.sqrt(10000/2)))]

    for i in range(25, 10000, 2):
        if i not in primes:
            possibles = [p for p in primes if p < i]
            works = False
            for p in possibles:
                test = i
                test -= p
                if test/2 == int(test/2) and test/2 in squares:
                    works = True
                    break
            if not works:
                return i
Beispiel #10
0
def eul51(n):
    """Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits)
    with the same digit, is part of an n = eight prime value family."""

    digits = 6  # for n = 8, assume 6 digit prime family
    primes = " ".join([str(i) for i in sieve(10**digits) if i > 10**(digits-1)])
    combos = product(range(2), repeat=digits)

    for combo in combos:
        if len(set(combo)) == 1:
            continue
        possibilities = []
        for i in range(10):
            reg = r""
            for digit in range(digits):
                if combo[digit] == 1:
                    reg += "["+str(i)+"]"
                else:
                    reg += "\d"
            p = re.compile(reg)
            possibilities.extend(p.findall(primes))

        for i in range(digits):
            if combo[i] == 1:
                n_p = []
                for p in possibilities:
                    l = list(p)
                    l[i] = "*"
                    n_p.append("".join(l))
                possibilities = n_p
        c = Counter(possibilities)
        if c.most_common(1)[0][1] == n:
            num = c.most_common(1)[0][0]
            reg = r""
            f = True
            for i in num:
                if i == "*":
                    if f:
                        reg += r"(\d)"
                        f = False
                    else:
                        reg += r"\1"
                else:
                    reg += r"["+str(i)+r"]"
            p = re.compile(reg)
            return min([x.group(0) for x in p.finditer(primes)])
Beispiel #11
0
def eul10():
    """Find the sum of all the primes below two million."""
    return sum(eul.sieve(2000000))
Beispiel #12
0
def eul10(n):
    """Find the sum of all the primes below n = two million."""
    return sum(eul.sieve(n))