Exemplo n.º 1
0
maxConsecutivePrimes = 40
result = 1 * 41

# b is prime > 2
primes = listOfPrimes(1000)[1:]

# a must be odd
for a in range (-999, 1000, 2):

    # bisect_left locate the insertion point to maintain sorted order
    # we will use it to find the index of a first element >= 2 - a in prime list to make sure that b >= 2 - a
    for b in primes[bisect_left(primes, 2 - a):]:

        # f(0) = b, b is guaranteed to be prime
        # let's start from n = 1
        n = 1
        consecutivePrimes = 1

        while True:
            if isPrime (n * n + a * n + b):
                consecutivePrimes += 1
            else:
                break
            n += 1

        if maxConsecutivePrimes < consecutivePrimes:
            maxConsecutivePrimes = consecutivePrimes
            result = a * b

print ("Result:", result)
Exemplo n.º 2
0
#!/usr/bin/env python3

# Pandigital prime
# 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?

# Solution:
# For every 5-, 6-, 8- and 9-digit pandigital number the sum of digits is divisible by 3, so the number itself is divisible by 3, see:
# http://en.wikipedia.org/wiki/Divisibility_rule#Divisibility_by_3_or_9
# So it is enough to check 7- and 4-digit pandigital numbers (2143 is a 4-digit prime pandigital number).

from itertools import permutations
from myUtils import isPrime
import sys

for n in [7, 4]:
    # generate list of digits in a descending order, to make sure permutations start from the biggest number
    digits = "".join([str(i) for i in range (n, 0, -1)])

    for number in [int(''.join(p)) for p in permutations(digits)]:
        if isPrime(number):
            print ("Result:", number)
            sys.exit()