Example #1
0
def answer():
    # ratios = []
    # for n in range(2, 10000000):
    #     # Euler's Product Formula
    #     primes = list(set(prime_factors(n)))
    #     product = 1
    #     for p in primes: 
    #         product *= (1 - (1/p))
    #     if is_perm(n, product*n):
    #         ratios.append(1/product)
    # return ratios.index(min(ratios)) + 2
    """
    The above solution would work, but it takes far longer than the minute required
    so the best is to analyze the code. It seems to minimize the ratio 1/product
    we should maximize the product and take advantage of the fact that it is the
    product of two large distinct primes close to the sqrt(10**7)
    """
    ratios = {}
    primes = primes_to(3800,2200)
    for a in range(len(primes)):
        for b in range(a+1, len(primes)):
            n = primes[a] * primes[b]
            if n < 10000000:
                phi = (primes[a] - 1) * (primes[b] - 1)
                ratio = n/phi
                if is_perm(n, phi):
                    ratios[ratio] = n
    return ratios[min(ratios.keys())]
Example #2
0
def answer():
    # Looking for 6-digit number with exactly 3 repeating digits not including
    # the last one since then the family could not contain 8 primes, furthermore
    # the smallest prime must have repeating digits 0, 1, or 2 in order for the 
    # family to have eight members
    primes = primes_to(1000000, 100000)
    for prime in primes:
        next = str(prime)
        if next.count('0') == 3 and prime_family(next, '0') \
        or next.count('1') == 3 and prime_family(next, '1') and next[:-1] != 1\
        or next.count('2') == 3 and prime_family(next, '2'):
            return next
Example #3
0
def answer():
    # Lower bound is 1487
    found = 0
    primes = primes_to(10000, 1487)
    for a in range(len(primes)):
        for b in range(a+1, len(primes)):
            prime_a = primes[a]
            prime_b = primes[b]
            c = prime_b + (prime_b - prime_a)
            if c < 10000 and is_prime(c):
                if is_perm(prime_a, prime_b) and is_perm(prime_a, c):
                    found = str(prime_a) + str(prime_b) + str(c)
                    break
        if found:
            break
    return found
Example #4
0
def answer(n):
    length = 0
    longest = 0
    primes = primes_to(n)
    # Make a list of sums
    prime_sum = [0]
    for x in range(len(primes)):
        prime_sum.append(prime_sum[x] + primes[x])
    for a in range(len(prime_sum)):
        b = a - (length + 1)
        while b >= 0:
            dif = prime_sum[a] - prime_sum[b]
            if dif > n:
                b -=1
                break
            if is_prime(dif):
                length = a - b
                longest = dif
            b -=1
    print("The prime is: " + str(longest) + " and consists of " + str(length) + " primes.")
Example #5
0
def answer():
    #ratios = []
    # for n in range(2, 10000000):
    #     # Euler's Product Formula
    #     primes = list(set(prime_factors(n)))
    #     product = 1
    #     for p in primes: 
    #         product *= (1 - (1/p))
    #     ratios.append(1/product)
    # return ratios.index(max(ratios)) + 2
    """
    Above code runs in 66 seconds so I reanalyzed it to see how to simplify it
    We are  essentially trying to maximize distinct primes to maximize the ratio, 
    so we should just multiply primes until we reach 1,000,000 
    """
    primes = primes_to(20)
    product = 1
    n = 0
    while product * primes[n] <= 1000000:
        product *= primes[n]
        n += 1
    return product 
Example #6
0
"""
Prime pair sets
Project Euler Problem #60
by Muaz Siddiqui

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.
"""
from euler_helpers import timeit, is_prime, primes_to
import itertools
prime_list = primes_to(8400)

def is_prime_set(primes):
    return all(is_prime(int(str(prime[0]) + str(prime[1]))) for prime in itertools.permutations(primes, 2))

def prime_pairs(primes, length):
    if len(primes) == length:
        return primes
    for prime in prime_list:
        check = primes + [prime]
        if prime > primes[-1] and is_prime_set(check):
            next = prime_pairs(check, length)
            if next:
                return next
    return 0