Ejemplo n.º 1
0
#!/usr/bin/python

from math import log
from math import sqrt
from euler import appendToPrimes
from euler import isPrime
from euler import appendDigitsUnsorted

N = 1000000  # N = 100 #
BASE = 10

primes = []
appendToPrimes(int(sqrt(N)), primes)
# print(primes) # [2, 3, 5, 7]

a = int(sqrt(N))
while a < N:
    if isPrime(a, primes):
        primes.append(a)
    a += 1

# primes contains all primes less than N.
# print(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]

possibles = [
]  # potentially right truncatable: no internal {0,2,4,5,6,8} and no left {0,4,6,8}
for p in primes:
    digits = appendDigitsUnsorted(p, [])
    if 0 in digits or 4 in digits or 6 in digits or 8 in digits:
        continue
    digits.pop(0)
Ejemplo n.º 2
0
#!/usr/bin/python

from euler import isPrime, appendToPrimes

PRIMES = []
UPPER = 10**5
appendToPrimes(UPPER, PRIMES)

twoSquares = [2 * n * n for n in range(0, UPPER)]

n = 3
while True:
    while isPrime(n, PRIMES):
        n += 2
    bad = True
    for j in twoSquares:
        if n <= j:
            break
        if isPrime(n - j, PRIMES):
            bad = False
            n += 2
            break
    if bad:
        break

print(n)
Ejemplo n.º 3
0
#!/usr/bin/python

import euler

n = 20

primes0 = [2, 3, 5, 7, 11, 13, 17, 19]
primes = []
euler.appendToPrimes(n, primes)
assert primes == primes0

n = 30

euler.appendToPrimes(n, primes)
primes0 += [23, 29]
assert primes == primes0

primeFactors0 = [2, 5, 23]
primeFactors = euler.primeFactors(230, primes)
assert primeFactors == primeFactors0

primeFactors0 = [2, 2, 3, 3]
primeFactors = euler.primeFactors(36, primes)
assert primeFactors == primeFactors0

primePowerFactorization0 = {2: 2, 3: 2}
primePowerFactorization = euler.primePowerFactorization(primeFactors)
assert primePowerFactorization0 == primePowerFactorization

assert (euler.descendingFactorial(5, 2) == 20)
assert (euler.descendingFactorial(5, 3) == 60)
Ejemplo n.º 4
0
#!/usr/bin/python

import euler

BOUND = 1000  # upper bound for testing primalities
TERMS = 3  # terms in quadratic
primes = set(euler.appendToPrimes(TERMS * BOUND * BOUND))


def quadratic(n, a, b):
    return n * n + a * n + b


def isPrime(n, a, b, primes):
    return quadratic(n, a, b) in primes


# Returns the count of prime values starting from n = 0.
def countOfPrimes(maximum, a, b, primes):
    n = 0
    while n <= maximum:
        if isPrime(n, a, b, primes):
            n += 1
        else:
            return n


maximumCount = 0  # max counter for primes

a = -BOUND + 1
while a < BOUND - 1:
Ejemplo n.º 5
0
#!/usr/bin/python

from itertools import permutations
from euler import appendToPrimes
from euler import isPrime

BASE = 10
D = 4  # 2143 is a pandigital prime

PRIMES = []
appendToPrimes(10**5, PRIMES)

maximum = 0
for n in range(D, BASE):
    for p in list(permutations(range(1, n + 1))):
        i = int(''.join(map(str, p)))
        if isPrime(i, PRIMES):
            if maximum < i:
                maximum = i

print(maximum)
Ejemplo n.º 6
0
#!/usr/bin/python

from itertools import permutations
from euler import appendToPrimes

BASE = 10

PRIMES = []
appendToPrimes(18, PRIMES)
assert (PRIMES[-1] == 17)
assert (len(PRIMES) == BASE - 3)


def isSubstringDivisible(n):
    for i in range(1, BASE - 2):
        triple = n % (BASE**3)
        if triple % PRIMES[-i] != 0:
            return False
        n //= BASE
    return True


strings = list(permutations(range(0, BASE)))
pandigitals = [int(''.join(map(str, s))) for s in strings]

print(sum([n for n in pandigitals if isSubstringDivisible(n)]))