Пример #1
0
Файл: p179.py Проект: icot/euler
def main2(N):
    print "Generating primes"
    maxp = int(sqrt(N)) + 1
    primes = FastPrimeSieve(maxp)
    print "Computing factors"
    prev = 0
    total = 0
    for n in xrange(2,int(1e7) +1):
        prime_factors = trial_division(n, primes)
        ndivs = ndivisors(n, prime_factors)
        if ndivs == prev:
            total += 1
        prev = ndivs
        if n % 100000 == 0:
            print n
    print total
Пример #2
0
def worker_file(a, b, primes):
    with gzip.open("dump.bin", "ab", compresslevel=3) as fp:
        buf = []
        for n in xrange(a, b):
            prime_factors = trial_division(n, primes)
            num_factors = len(prime_factors)
            data = {
                "n": n,
                "factors": prime_factors,
                "ndivisors": ndivisors(n, prime_factors),
                "nfactors": num_factors,
                "max_factor": max(prime_factors),
                "primality": num_factors == 1,
            }
            buf.append(data)
        cPickle.dump(buf, fp, protocol=2)
    return b - a
Пример #3
0
Файл: p179.py Проект: icot/euler
def worker_db(a,b, primes):
    with Connection('localhost') as con:
        db = con['integers']
        col = db['misc']
        buf = []
        for n in xrange(a, b):
            prime_factors = trial_division(n, primes)
            num_factors = len(prime_factors)
            data = {'n' : n, 
                    'factors' : prime_factors,
                    'ndivisors' : ndivisors(n, prime_factors),
                    'nfactors' : num_factors,
                    'max_factor': max(prime_factors),
                    'primality' : num_factors == 1}
            buf.append(data)
        col.insert(buf)
        con.disconnect()
    return (b-a)