Sunday, July 25, 2010

Sieve of Aristophane!

Sieve of Aristaphane, I have conquered you!

After f'ing around at odd times for the past week, and getting a little frustrated, I suddenly programmed a functioning Sieve of Aristaphane in 20 minutes.

Code is below.

#The Sieve of Eristaphanes



high_end = int(raw_input("What range of numbers should we examine? From\ one to ______?"))
high_end = high_end + 1
the_range = range(2,high_end)
the_range = list(the_range)
the_primes = the_range

divisor = 2
global divisor

def eliminate_non_primes():
for x in the_range:
if x%divisor == 0 and x != divisor:
the_primes.remove(x)
divisor +=1
global divisor

while divisor < high_end/2:
eliminate_non_primes()

print "Divisor equals",divisor


print the_primes

No comments:

Post a Comment

Followers