예제 #1
0
파일: p050.py 프로젝트: nonette/euler
def p050(N=10**6, lim=4000):
    primes = numty.sieve(lim)
    primeset = set(primes)
    bestprime = 0
    bestlen = 0
    bestbase = 0
    i = 0
    while True:
        if i + bestlen > len(primes):
            logger.error('Limit was too low!')
            return
        cand = sum(primes[i:i + bestlen])
        if cand >= N:
            break
        j = i + bestlen
        while cand < N:
            if j >= len(primes):
                logger.error('Limit was too low!')
                return
            cand += primes[j]
            j += 1
        while j > i + bestlen:
            if numty.isprime(cand):
                bestprime = cand
                bestlen = j - i
                bestbase = primes[i]
                break
            cand -= primes[j - 1]
            j -= 1
        i += 1
    logger.debug((bestlen, primes[bestbase:bestbase + bestlen]))
    return bestprime
예제 #2
0
파일: p027.py 프로젝트: nonette/euler
def p027(N=10**3):
    lim = 10 * N
    table = set(numty.sieve(lim))
    primes = numty.sieve(2 * N + 1)

    best = 0
    best_ab = (0, 0)

    for b in primes:
        if b > N:
            break
        for c in primes:
            a = c - b - 1
            if a > N:
                break
            count = 2
            while True:
                cand = count**2 + a * count + b
                if cand < 0:
                    break
                if cand < lim and cand not in table:
                    break
                if cand >= lim and not numty.isprime(cand):
                    break
                count += 1
            if count > best:
                best_ab = (a, b)
                best = count
    return best_ab[0] * best_ab[1]
예제 #3
0
파일: p050.py 프로젝트: nonette/euler
def p050(N=10 ** 6, lim=4000):
    primes = numty.sieve(lim)
    primeset = set(primes)
    bestprime = 0
    bestlen = 0
    bestbase = 0
    i = 0
    while True:
        if i + bestlen > len(primes):
            logger.error("Limit was too low!")
            return
        cand = sum(primes[i : i + bestlen])
        if cand >= N:
            break
        j = i + bestlen
        while cand < N:
            if j >= len(primes):
                logger.error("Limit was too low!")
                return
            cand += primes[j]
            j += 1
        while j > i + bestlen:
            if numty.isprime(cand):
                bestprime = cand
                bestlen = j - i
                bestbase = primes[i]
                break
            cand -= primes[j - 1]
            j -= 1
        i += 1
    logger.debug((bestlen, primes[bestbase : bestbase + bestlen]))
    return bestprime
예제 #4
0
파일: p027.py 프로젝트: nonette/euler
def p027(N = 10**3):
    lim = 10*N
    table = set(numty.sieve(lim))
    primes = numty.sieve(2*N+1)

    best = 0
    best_ab = (0,0)

    for b in primes:
        if b > N:
            break
        for c in primes:
            a = c - b - 1
            if a > N:
                break
            count = 2
            while True:
                cand = count**2+a*count+b
                if cand < 0:
                    break
                if cand < lim and cand not in table:
                    break
                if cand >= lim and not numty.isprime(cand):
                    break
                count += 1
            if count > best:
                best_ab = (a,b)
                best = count
    return best_ab[0]*best_ab[1]
예제 #5
0
파일: p058.py 프로젝트: nonette/euler
def p058(target=0.1):
    primecount = 0
    n = 1
    while True:
        corners = [(2 * n - 1) ** 2 + 2 * n, (2 * n - 1) ** 2 + 4 * n, (2 * n - 1) ** 2 + 6 * n]
        primecount += sum(1 for corner in corners if numty.isprime(corner))
        if 1.0 * primecount / (4 * n + 1) < target:
            return 2 * n + 1
        n += 1
예제 #6
0
def p058(target=0.1):
    primecount = 0
    n = 1
    while True:
        corners = [(2 * n - 1)**2 + 2 * n, (2 * n - 1)**2 + 4 * n,
                   (2 * n - 1)**2 + 6 * n]
        primecount += sum(1 for corner in corners if numty.isprime(corner))
        if 1.0 * primecount / (4 * n + 1) < target:
            return 2 * n + 1
        n += 1
예제 #7
0
파일: p060.py 프로젝트: nonette/euler
def gengraph(N):
    primes = numty.sieve(N)
    graph = defaultdict(list)
    for i in xrange(len(primes)):
        p = primes[i]
        for j in xrange(i):
            q = primes[j]
            if (p+q)%3 == 0:
                continue
            pq = int(str(p)+str(q))
            qp = int(str(q)+str(p))
            if numty.isprime(pq) and utils.isprime(qp):
                    graph[p].append(q)
                    graph[q].append(p)
    return graph
예제 #8
0
파일: p037.py 프로젝트: nonette/euler
def p037():
    primes = [2,3,5,7]
    allprimes = []
    while primes:
        logger.debug(primes)
        newprimes = []
        for prime in primes:
            for d in [1,3,7,9]:
                if numty.isprime(10*prime+d):
                    newprimes.append(10*prime+d)
        primes = newprimes
        allprimes += primes
    acc = 0
    count = 0
    for prime in allprimes:
        if truncatable(prime):
            logger.debug(prime)
            count += 1
            acc += prime
    logger.debug(count)
    return acc
예제 #9
0
def p041():
    for N in range(8,0,-1):
        for p in itertools.permutations(range(N,0,-1)):
            n = sum(d*10**(len(p)-1-i) for (i,d) in enumerate(p))
            if numty.isprime(n):
                return n
예제 #10
0
파일: p037.py 프로젝트: nonette/euler
def truncatable(prime):
    str_p = str(prime)
    for n in range(len(str_p)):
        if not numty.isprime(int(str_p[n:])):
            return False
    return True