Example #1
0
File: 37.py Project: domino14/euler
from eratosthenes import sieve

# How far to go?
primes = sieve(1000000)
prime_set = set(primes)
print 'generated primes'
num_primes = 0

s = 0

for p in primes:
    if p <= 7:
        continue

    # truncate right
    passed = True
    p_str = list(str(p))
    while len(p_str) >= 2:
        p_str.pop()
        if int(''.join(p_str)) not in prime_set:
            passed = False
            break
    if not passed:
        continue
    # truncate left
    passed = True
    p_str = list(str(p))
    while len(p_str) >= 2:
        p_str.pop(0)
        if int(''.join(p_str)) not in prime_set:
            passed = False
Example #2
0
File: 27.py Project: domino14/euler
# Find numbers a, b of the form n^2 + an + b, where the most primes are
# produced with consecutive values of n starting with 0,
# for a, b ranging between -1000 and 1000

from eratosthenes import sieve

# go up to 1000^2 + 1000*1000 + 1000 == 2001000
limit = 2001000
primes = set(sieve(limit))

max_n = 0
the_a = None
the_b = None
for a in range(-1000, 1001):
    for b in range(-1000, 1001):
        n = 0
        eq = n*n + a*n + b
        while eq in primes:
            n += 1
            eq = n*n + a*n + b
        if n > max_n:
            max_n = n
            the_a = a
            the_b = b

print max_n, the_a, the_b
print the_a * the_b
Example #3
0
generated by a single quadratic q. 

These bounds on a, b, and n imply q(n) < 3e6. Say 4e6, just to be safe.

The fastest way to check primality of numbers in the 10^6 range is to enumerate
them all with a sieve.
'''


if path.exists(__cache__):
    primes = np.load(__cache__)
    is_prime = np.zeros((5e6), dtype='bool')
    is_prime[primes] = True
else:
    is_prime = np.ones((5e6), dtype='int8')
    eratosthenes.sieve(is_prime)
    is_prime.dtype = 'bool'
    primes = np.where(is_prime)[0]
    np.save(__cache__, primes)

def candidates(max_a, max_b):
    '''
    candidates(int max_a=1000, int max_b=1000) yields (int, int)

    Given the bounds |a| < max_a and |b| < max_b, yields candidates for
    (a, b) as described above.
    '''

    for b in takewhile(lambda x: x < max_b, primes):
        for q in takewhile(lambda x: x < 1 + max_a + b, primes):
            yield (q - 1 - b, b)
Example #4
0
File: 50.py Project: domino14/euler
from eratosthenes import sieve

s = sieve(1000000)
prime_set = set(s)
summands = sieve(50000)

consecutives = 0
the_prime = 0

print 'evaluating', len(summands), 'summands'
for i, p in enumerate(summands):
    p_list = []
    for j in range(i, len(summands)):
        p_list.append(summands[j])
        the_sum = sum(p_list)
        if the_sum in prime_set:
            if len(p_list) > consecutives:
                consecutives = len(p_list)
                the_prime = the_sum


print the_prime, consecutives
Example #5
0
File: 51.py Project: domino14/euler
from eratosthenes import sieve
import itertools

s = sieve(1000000)
prime_set = set(s)


def replace(s, i, n):
    """
    Replace the character at index i in the string s with the value n

    >>> replace('12345', 0, '8')
    '82345'

    >>> replace('12345', 1, '8')
    '18345'

    >>> replace('12345', 2, '8')
    '12845'

    >>> replace('12345', 3, '8')
    '12385'

    >>> replace('12345', 4, '8')
    '12348'
    """
    return s[:i] + n + s[i+1:]


def num_primes_in_family(x):
    """
Example #6
0
File: 46.py Project: domino14/euler
from eratosthenes import sieve
import math
limit = 10e6
primes = set(sieve(int(limit)))
print 'genned primes'


def decompose(n):
    """
    Decompose the composite number n into the sum of a prime and twice a
    square. Return whether it was successful.

    2 x i^2 + p = n
    2 x i^2 = n - p
    2 x i^2 <= n - 2

    i^2 <= ((n-2)/2)
    i < sqrt((n-2)/2)
    """
    l = int(math.ceil(math.sqrt((n-2)/2.)))
    for p_sq in range(1, l):
        if n - (2 * p_sq * p_sq) in primes:
            return True

    return False

for n in range(37, int(limit), 2):
    if n % 2 == 0:
        continue
    if n in primes:
        continue
Example #7
0
File: 48.py Project: domino14/euler
from eratosthenes import sieve

s = set(sieve(10000))
inc = 3330
for i in range(999, 10000, 2):
    i2 = i + 3330
    i3 = i + 6660
    if i in s and i2 in s and i3 in s:
        si1 = set(list(str(i)))
        si2 = set(list(str(i2)))
        si3 = set(list(str(i3)))

        if si1 == si2 and si2 == si3:
            print '{0}{1}{2}'.format(i, i+3330, i+6660)
Example #8
0
import numpy as np
import pyximport
pyximport.install()

import eratosthenes

A = np.ones(5000000, dtype='int8')
eratosthenes.sieve(A)
A.dtype = 'bool'
assert not A[41 * 41]
print np.where(A)[0]