コード例 #1
0
ファイル: prime.py プロジェクト: durbanie/ProjectEuler
def sum_primes_up_to_N(N):
    from stopwatch import StopWatch
    StopWatch.start()
    _generator = Prime()
    _primes = _generator.get_primes(N)
    print sum(_primes)
    StopWatch.print_time()
コード例 #2
0
ファイル: prime.py プロジェクト: durbanie/ProjectEuler
def get_Nth_prime(N):
    from stopwatch import StopWatch
    StopWatch.start()
    _generator = Prime()
    _primes = _generator.get_first_N_primes(N)
    print _primes[N-1]
    StopWatch.print_time()
コード例 #3
0
ファイル: prime.py プロジェクト: durbanie/ProjectEuler
def check_up_to_N(N):
    from stopwatch import StopWatch
    StopWatch.start()
    _checker = Prime()
    #_checker.get_primes(N)
    for i in range(N):
        if i % 10000 == 0: print i
        if _checker.is_prime(i):
            if N < 10000:
                print i, 
    print ""
    StopWatch.print_time()    
コード例 #4
0
ファイル: sieves.py プロジェクト: durbanie/ProjectEuler
#import psyco; psyco.full()
from math import sqrt, ceil

def sieveOfEratosthenes(n):
    """sieveOfEratosthenes(n): return the list of the primes < n."""
    # Code from: <*****@*****.**>, Nov 30 2006
    # http://groups.google.com/group/comp.lang.python/msg/f1f10ced88c68c2d
    if n <= 2:
        return []
    sieve = range(3, n, 2) # is there a way we can do this
    top = len(sieve)
    for si in sieve:
        if si:
            bottom = (si*si - 3) // 2 # index of the square of si
            if bottom >= top:
                break
            num_zeros = -((bottom - top) // si)
            sieve[bottom::si] = [0] * num_zeros
    return [2] + [el for el in sieve if el]

if __name__=='__main__':
    
    n=10000000
    
    from stopwatch import StopWatch
        
    StopWatch.start()
    sieveOfEratosthenes(n)
    print "Sieve of Eratosthenes: ",
    StopWatch.print_time()