Esempio n. 1
0
def make_factorizer( maxn ) :
    primes = [ p for p in build_sieve(maxn) if p ]
    return lambda n : factorize2( prime_factors2( n, primes ) )
Esempio n. 2
0
where |n| is the modulus/absolute value of n
e.g. |11| = 11 and |4| = 4
Find the product of the coefficients, a and b,
for the quadratic expression that produces the maximum
number of primes for consecutive values of n, starting with n = 0.
"""

from pe10 import build_sieve
from itertools import takewhile

def qf(n,a,b) :
    return n**2 + a*n + b

maxab = 1000

sieve = build_sieve(qf(maxab, maxab, maxab)) #at n=a=b it will not be prime that's for sure

is_prime = lambda n : sieve[n] != 0

r = xrange(-maxab+1,maxab)

gen_n = lambda a,b : takewhile( lambda n : is_prime(n),
                                (qf(i,a,b) for i in xrange(maxab)) )

assert len(list(gen_n(1,41))) == 40
assert len(list(gen_n(-79,1601))) == 80   

s = max( (len(list(gen_n(a,b))), a,b) for a in r for b in r )
s = s[1] * s[2]

assert s == -59231
Esempio n. 3
0
"""
http://projecteuler.net/index.php?section=problems&id=35

The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?
"""

from pe10 import build_sieve

sieve = build_sieve(10**6)

def rotations( n ) :
    digits = str(n)
    return set(int( digits[x:]+digits[:x] ) for x in xrange(len(digits)))

def gen_circular(sieve) :
    s = sieve[:]   #copy 
    for i in xrange(len(s)):
        p = s[i]
        if p :            
            is_circular = True
            pr = rotations(p)
            for r in pr :
                if not s[r] :
                    is_circular = False
                s[r] = 0 # don't check the permutations
            if is_circular :
                for r in pr :