Пример #1
0
# Problem 41
#
# We shall say that an n-digit number is pandigital if it makes use of
# all the digits 1 to n exactly once. For example, 2143 is a 4-digit
# pandigital and is also prime.
#
# What is the largest n-digit pandigital prime that exists?

import eutils.primes as primes
import eutils.strings as strings
import time

start = time.time()

m = 9
found = False
while not found:
    digits = list(range(m - 1, 0, -1))
    perms = [int(str(m) + x) for x in strings.l_collapse(strings.l_permute(digits))]
    p = [x for x in perms if primes.is_prime(x)]
    if len(p):
        found = True
    m -= 1


ans = p[0]

end = time.time()
print("The answer to Problem 41 is: %s" % ans)
print("<< Returned in %s seconds >>" % (end - start))
Пример #2
0
#
# The longest sum of consecutive primes below one-thousand that adds to
# a prime, contains 21 terms, and is equal to 953.
#
# Which prime, below one-million, can be written as the sum of the most
# consecutive primes?

import eutils.primes as primes
import eutils.vectorize as vec
import time

start = time.time()

ceil = 1000000

p = [2] + [n for n in list(range(3, ceil, 2)) if primes.is_prime(n)]

max = [0, 0]
for i in range(0, len(p)):
    j = i + 1 + max[1]
    ssum = vec.v_sum(p[i:j])
    while j <= len(p) and ssum < ceil:
        if primes.is_prime(ssum):
            max = [ssum, len(p[i:j])]
        j += 1
        ssum = vec.v_sum(p[i:j])

ans = max[0]

end = time.time()
print("The answer to Problem 50 is: %s" % ans)
Пример #3
0
# Problem 3
#
# The prime factors of 13195 are 5, 7, 13 and 29.
#
# What is the largest prime factor of the number 600851475143?

import eutils.factors as factors
import eutils.primes as primes
import time
start = time.time()

ans = None
fac = factors.l_factors(600851475143)

i = len(fac) - 2
while ans is None:
    if primes.is_prime(fac[i]):
        ans = fac[i]
    i -= 1

end = time.time()
print("The answer to Problem 3 is: %s" % ans)
print("<< Returned in %s seconds >>" % (end - start))
Пример #4
0
# 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 10 001st prime number?

import eutils.primes as primes
import time
start = time.time()

count = 1
n = 3
while count < 10001:
    if primes.is_prime(n):
        ans = n
        count += 1
    n += 2

end = time.time()
print("The answer to Problem 7 is: %s" % ans)
print("<< Returned in %s seconds >>" % (end - start))