Skip to content

Instantly share code, notes, and snippets.

@flavioamieiro
Created March 27, 2010 01:05
Show Gist options
  • Save flavioamieiro/345602 to your computer and use it in GitHub Desktop.
Save flavioamieiro/345602 to your computer and use it in GitHub Desktop.
# 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