Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 2 | |
3 | # Print prime numbers in a given range | ||||
4 | |||||
5 | def main(): | ||||
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 6 | import sys |
7 | min, max = 2, 0x7fffffff | ||||
8 | if sys.argv[1:]: | ||||
9 | min = int(eval(sys.argv[1])) | ||||
10 | if sys.argv[2:]: | ||||
11 | max = int(eval(sys.argv[2])) | ||||
12 | primes(min, max) | ||||
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 13 | |
14 | def primes(min, max): | ||||
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 15 | if 2 >= min: print(2) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 16 | primes = [2] |
17 | i = 3 | ||||
18 | while i <= max: | ||||
19 | for p in primes: | ||||
20 | if i%p == 0 or p*p > i: break | ||||
Neal Norwitz | 3bd844e | 2006-08-29 04:39:12 +0000 | [diff] [blame] | 21 | if i%p != 0: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 22 | primes.append(i) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 23 | if i >= min: print(i) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 24 | i = i+2 |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 25 | |
Johannes Gijsbers | 7a8c43e | 2004-09-11 16:34:35 +0000 | [diff] [blame] | 26 | if __name__ == "__main__": |
27 | main() |