I'm an absolute novice to python but I am trying to compute something that should act like the sieve of Eratosthenes.
I'm going to start easy and just create a set with all integers from 2 up to 100. Let's call that set S.
I then want to create a set of all integers n such that 2n is contained in that set, let's call it P1. In other words, a set of the integers 2, 4, 6, 8 etc.
I want to then do the same thing but with P2 = 3n and then P3 = 5n.
In the end I want to return all integers of my set S but disregard the integers in P1, P2 and P3.
How would I go on about doing this?
My try:
numbers=set(range(2,100))
but I'm stuck on creating the other sets and disregarding them!
Thanks.
My idea so far:
def sieve(n):
S = set(range(2, 100))
P1 = set(range(0, 100, 2))
P2 = set(range(0, 100, 3))
P3 = set(range(0, 100, 5))
P5 = set(range(0, 100, 7))
return S-P1-P2-P3-P5
print (sieve(100))