Example #1
0
def p60():
    """ The 5-prime set with the least sum s.t concatenation
    of any pair of primes in any order results in a prime"""

    # we start with n = 100000
    pslist = primes.getPrimeList(100000)
    matchingPrimes = {}

    def add(p, q):
        if p not in matchingPrimes:
            matchingPrimes[p] = set()
        matchingPrimes[p].add(q)
        return

    def recurse(ps):
        if len(ps) == 5:
            return sum(ps)

        ret = 10**100
        p1 = ps[0]

        if p1 not in matchingPrimes:
            return ret

        for q in matchingPrimes[p1]:
            matchesAll = True
            for p in ps[1:]:
                if q not in matchingPrimes[p]:
                    matchesAll = False
                    break

            if matchesAll:
                ret = min(ret, recurse(ps + [q]))

        return ret

    for p in pslist:
        for q in pslist:
            if p >= q:
                break
            pq = int(str(p) + str(q))
            qp = int(str(q) + str(p))
            if primes.checkIfPrime(qp) and primes.checkIfPrime(pq):
                add(p, q)
                add(q, p)

    ret = 1 << 100
    for p in pslist:
        if p in matchingPrimes:
            ret = min(ret, recurse([p]))

    return ret
Example #2
0
def p3():
    """ Largest prime factor of the number 600851475143"""
    number = 600851475143
    squareRoot = int(number**0.5)

    ret = 0
    for s in xrange(2, squareRoot + 1):
        if number % s == 0:
            if primes.checkIfPrime(s):
                ret = max(ret, s)

            while number % s == 0:
                number = number / s

    if number != 1 and primes.checkIfPrime(number):
        ret = max(ret, number)
    return ret
Example #3
0
    def testFewKnownPrimes(self):
        known_primes = [
            2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,
            67, 71, 73, 79, 83, 89, 97
        ]

        for n in xrange(1, 101):
            inclusion = n in known_primes
            primality = primes.checkIfPrime(n)
            self.assertTrue(not (inclusion ^ primality))
Example #4
0
def p41():
    """ Largest pandigital prime """
    res = 0
    for d in xrange(4, 9):
        digits = range(1, d + 1)
        allPerms = combinatorics.genAllPerms(digits)
        for perm in allPerms:
            num = 0
            for d in perm:
                num = 10 * num + d

            if primes.checkIfPrime(num):
                res = max(res, num)

    return res
Example #5
0
def getLen(a, b):
    n = 0
    while primes.checkIfPrime(n * n + a * n + b):
        n = n + 1
    return n