示例#1
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]
示例#2
0
文件: euler.py 项目: jdp/euler
def euler_25():
    """What is the first term in the Fibonacci sequence to contain 1000 digits?"""
    from itertools import takewhile
    from eutil import fib, ilen

    return ilen(takewhile(lambda x: len(str(x)) < 1000, fib()))