-
Notifications
You must be signed in to change notification settings - Fork 0
/
primes.py
executable file
·59 lines (44 loc) · 1.43 KB
/
primes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python2
from sys import stdout, argv, exit
RATE = 5000
def percentProgress(step, total):
return 100.0 * (float(step) / float(total))
def countPrimes(maxVal):
nPrimes = 0
primes = []
for i in xrange(2, maxVal):
prime = True
for p in primes:
# Exit loop if not prime or not going to find factor
if (p * p > i):
break
# Exit loop if proven not prime
if ((i % p) == 0):
prime = False
break
if prime:
primes.append(i)
nPrimes += 1
if (nPrimes < 100):
stdout.write("{:3}, ".format(i))
if not nPrimes % 5:
stdout.write("\n")
elif (nPrimes == 100):
stdout.write("{:3}\n".format(i))
if ((nPrimes % RATE) == 0):
stdout.write("\033[GFound {:12d} at {:7.3f}%...".format(
nPrimes, percentProgress(i, maxVal)
))
stdout.flush()
if nPrimes < 100 and primes % 5:
stdout.write("\n")
stdout.write("\033[G\033[KFound {:12d} primes.\n".format(nPrimes))
if __name__ == "__main__":
if len(argv) != 2:
print "Usage: {} max_prime".format(argv[0])
exit(1)
maxVal = int(argv[1])
if (maxVal <= 0):
print "max_val must be a positive integer"
exit(1)
countPrimes(maxVal)