Created
March 27, 2010 01:05
-
-
Save flavioamieiro/345602 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Implementation of the sieve of Eratostenes. | |
# Calculates all the prime numbers in a range up to the specified limit. | |
import math | |
import sys | |
def process(squareRoot, list): | |
"""This function calculates all the prime numbers in a range (from 2 to the | |
limit you specify). | |
Takes as arguments: | |
squareRoot - the square root of the highest number rounded down | |
list - a range(2, limit_number) | |
""" | |
count = 0 | |
while count <= squareRoot: | |
for i in list[count+1:]: | |
result = i % list[count] | |
if result == 0: | |
list.remove(i) | |
count = count + 1 | |
return list | |
# If the program is directly called, not by another program, then print a list | |
# of the prime numbers found. | |
if __name__ == '__main__': | |
try: | |
limit = int(sys.argv[1]) | |
except IndexError: | |
limit = input('qual o numero limite do calculo a ser feito? ') | |
list = range(2,limit) | |
squareRoot = int(math.sqrt(limit)) | |
process(squareRoot, list) | |
for item in list: | |
print item |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment