コード例 #1
0
def main():
    diff = 3330
    seen = set([1487, 4817, 8147])
    primeset = set(primes(10000))
    for p in primes(10000):
        if (p < (1000 + diff)) or ('0' in str(p)) or (p in seen):
            continue
        if (p - diff) in primeset and (p + diff) in primeset:
            if is_permutation(p, p - diff) and is_permutation(p, p + diff):
                return ''.join(map(str, [p - diff, p, p + diff]))
    return None
コード例 #2
0
ファイル: euler.py プロジェクト: jdp/euler
def euler_35():
    """How many circular primes are there below one million?"""
    from itertools import takewhile
    from eutil import primes, rotations

    ps = set(takewhile(lambda p: p < 1000000, primes()))
    return len(filter(None, [all((x in ps) for x in [int(r) for r in rotations(str(p))]) for p in ps]))
コード例 #3
0
ファイル: euler.py プロジェクト: jdp/euler
def euler_3():
    """Find the largest prime factor of a composite number."""
    from math import sqrt
    from itertools import takewhile
    from eutil import primes

    num = 600851475143
    ps = takewhile(lambda p: p < sqrt(num), primes())
    return max([p for p in ps if num % p == 0])
コード例 #4
0
ファイル: euler.py プロジェクト: jdp/euler
def euler_69():
    """Find the value of n ≤ 1,000,000 for which n/φ(n) is a maximum."""
    from eutil import primes

    n = 1
    for p in primes():
        if (n * p) > 1000000:
            break
        n = n * p
    return n
コード例 #5
0
ファイル: euler.py プロジェクト: jdp/euler
def euler_26():
    "Find the value of d < 1000 for which 1/d contains the longest recurring cycle."
    from itertools import count, dropwhile, takewhile
    from eutil import primes

    ps = list(takewhile(lambda x: x < 1000, primes()))
    for p in reversed(ps):
        c = next(dropwhile(lambda x: (pow(10, x) - 1) % p != 0, count(start=1)))
        if p - c == 1:
            return p
コード例 #6
0
ファイル: euler.py プロジェクト: jdp/euler
def euler_27():
    "Find a quadratic formula that produces the maximum number of primes for consecutive values of n."
    from itertools import chain, imap, product, takewhile, tee
    from eutil import ilen, primes, is_prime, sequence

    a_s = xrange(-999, 1000)
    ps = tee(takewhile(lambda p: p < 1000, primes()))
    b_s = chain(ps[0], imap(lambda x: -x, ps[1]))
    seqs = [(ilen(takewhile(is_prime, sequence(lambda n: n ** 2 + a * n + b))), a, b) for a, b in product(a_s, b_s)]
    seq = max(seqs, key=lambda t: t[0])
    return seq[1] * seq[2]
コード例 #7
0
ファイル: problem50.py プロジェクト: devmacrile/euler
def main():
    n = 1000000
    primelist = primes(5000)
    longest = []
    for i in range(len(primelist)):
        for j in range(i + len(longest), len(primelist)):
            if sum(primelist[i:j]) > n:
                break
            if (j - i) > len(longest):
                if is_prime(sum(primelist[i:j])):
                    longest = primelist[i:j]
    return len(longest), sum(longest)
コード例 #8
0
ファイル: problem50.py プロジェクト: devmacrile/euler
def main():
    n = 1000000
    primelist = primes(5000)
    longest = []
    for i in range(len(primelist)):
        for j in range(i + len(longest), len(primelist)):
            if sum(primelist[i:j]) > n:
                break
            if (j - i) > len(longest):
                if is_prime(sum(primelist[i:j])):
                    longest = primelist[i:j]
    return len(longest), sum(longest)
コード例 #9
0
ファイル: euler.py プロジェクト: jdp/euler
def euler_10():
    """Calculate the sum of all the primes below two million."""
    from itertools import takewhile
    from eutil import primes

    return sum(takewhile(lambda p: p < 2000000, primes()))
コード例 #10
0
ファイル: euler.py プロジェクト: jdp/euler
def euler_7():
    """Find the 10001st prime."""
    from eutil import primes, nth

    return nth(primes(), 10000)