厄拉多塞素数筛选法(primesieve.py)

 1 import sys
 2 import stdarray
 3 import stdio
 4 n = int(sys.argv[1]);
 5 isPrime = stdarray.create1D(n+1, True);
 6 for i in range(2,n+1):
 7     if isPrime[i]:
 8         #Mark multiples of i as nonprime.
 9         for j in range(2,n//i+1):
10             isPrime[i*j]=False;
11 # Count the primes.
12 count = 0
13 for i in range(2,n+1):
14     if isPrime[i]:
15         count += 1
16 stdio.write(count)

 

posted @ 2017-08-02 13:12  跳过了E  阅读(355)  评论(0编辑  收藏  举报