Esempio n. 1
0
def euler():
    # highest number of consecutive primes that add up to a prime
    highest_chain_length = 1
    # highest resulting sum that was found
    highest_chain = 2
    # for each prime
    for prime_number in prime.primes(MAX):
        # exit when it can't possibly be the start of a new chain
        if prime_number > MAX // highest_chain_length:
            break
        # start a chain from this prime number
        accumulator = prime_number
        # length of the current chain
        chain_length = 1
        # for each prime starting from here
        for chain_prime in prime.primes(MAX):
            # exit when outside the bounds
            if accumulator > MAX:
                break
            # skip the primes below the "prime_number" start
            if chain_prime > prime_number:
                # if it's a prime and the chain length is longer than the
                # longest chain so far
                if prime.is_prime(accumulator) and \
                        chain_length > highest_chain_length:
                    highest_chain_length = chain_length
                    highest_chain = accumulator
                # increase chain length and add the next prime
                chain_length += 1
                accumulator += chain_prime
    return highest_chain
Esempio n. 2
0
def euler():
    # for each "starting" prime number
    for prime_number in prime.primes(200000):
        # list of integers for each digit
        prime_number_digits = list(int(digit) for digit in str(prime_number))
        # set (without duplicates) of the digits in the prime number
        prime_number_digit_set = set(prime_number_digits)
        # for each digit that could be replaced in the prime number
        for base_digit in prime_number_digit_set:
            # number of digit replacements that are actual prime numbers
            prime_count = 0
            # never replace the first digit with a zero
            replacements = range(10) if prime_number_digits[0] != base_digit \
                    else range(1, 10)
            # for each possible digit replacement
            for replacement_digit in replacements:
                # replace the digit base_digit with replacement_digit
                modified_digits = replace(prime_number_digits, base_digit,
                        replacement_digit)
                # convert that list to a number
                modified_number = int(''.join(str(digit) \
                        for digit in modified_digits))
                # if it's a prime, increment the prime count (duh)
                if prime.is_prime(modified_number):
                    prime_count += 1
            # return if the answer if we found it
            if prime_count == FAMILY_SIZE:
                return prime_number
Esempio n. 3
0
def euler():
    # set of matches found
    matches = set()
    # the primes can never exceed this value
    limit = math.ceil(math.sqrt(MAX))
    # for each (a,b,c) triplet such that a^2+b^3+c^4 < MAX
    for a in prime.primes(limit):
        if a * a >= MAX:
            break
        for b in prime.primes(limit):
            if a * a + b * b * b >= MAX:
                break
            for c in prime.primes(limit):
                k = a * a + b * b * b + c * c * c * c
                if k >= MAX:
                    break
                matches.add(k)
    # return the number of matches
    return len(matches)
Esempio n. 4
0
def euler():
    # return value, result
    best_match = 1
    # the answer will be the integer <= HIGHEST_N, which has the most prime
    # factors.
    # so just multiply primes together, until the product exceeds the limit.
    for p in primes(HIGHEST_N):
        if best_match * p > HIGHEST_N:
            break
        best_match *= p
    # return the answer
    return best_match
Esempio n. 5
0
def euler():
    # for each odd number starting at 3
    composite = 3
    while True:
        # 'found' will be True iff the composite can be expressed as the sum
        # of a prime and two times a square
        found = False
        # try all prime numbers that are less than the composite number
        for prime in primeutils.primes(composite):
            # calculate the square
            square = (composite - prime) // 2
            # it must have an integer square root for it to be valid
            root = math.sqrt(square)
            if root == int(root):
                found = True
                break
        # if it cannot be expressed as the sum of blah blah...
        if not found:
            # we found the answer
            return composite
        # next odd number
        composite += 2
Esempio n. 6
0
def prime_sets():
    """Yield all sets of possible answers for problem #49.

    Will 'yield' three arguments: the first, second and third prime.

    These three prime numbers are separated by exactly LEAP, and are
    permutations of each other. The three primes have exactly 4 digits."""
    # for each prime number
    for first_prime in prime.primes(10 ** DIGITS // 3):
        # ensure that it has enough digits
        if first_prime > 10 ** (DIGITS - 1):
            # calculate the other two primes
            second_prime = first_prime + LEAP
            third_prime = first_prime + 2 * LEAP
            # invalid if they're not permutations of eachother
            if not is_int_permutation(first_prime, second_prime) or \
                    not is_int_permutation(first_prime, third_prime):
                continue
            # invalid if they're not actually primes
            if not prime.is_prime(second_prime) or \
                    not prime.is_prime(third_prime):
                continue
            # if this is reached, the three primes make up a possible answer
            yield first_prime, second_prime, third_prime
Esempio n. 7
0
def euler():
    # calculate and return the sum of the primes
    return sum(prime.primes(MAX))
Esempio n. 8
0
def euler():
    accumulator = 0
    for prime in primeutils.primes(MAX):
        if is_circular(prime):
            accumulator += 1
    return accumulator