Have a good evening!
The Aim: to obtain a Sieve of Erastothenes
The Means: to build a function of a number n that the prime numbers up to n.
The code supposed to make this
from math import sqrt
def holeofStrainer():
"""The purpose is to build a sieve of Erasthotenes.
We beginn with a sieve without holes, called biglist.
The indexes of the list have a double function:
First, The natural list indexation. II-st, there are
as keys of the corresponding list items(the boolean
values of the prime being question about each number).
We have for each index a"""
bigList = [False, False] + [True]*100
print("line 11 - bigList : ", bigList)
for num in range(3, 101):
print("line 13 - num : ", num)
# I f we don't find divisors of nume littler that sqrt(num)
# it is guaranted that num is prime.
# 'int(sqrt(num)) + 1' is the first integer greater than sqrt(num)
for x in range(bigList[2], bigList[int(sqrt(num)) + 1]):
print("line 18 x is equal to %d"%x)
if num % x == 0:
print("line 20 {0} divided by {1} = {2} ".format(num, x, num/x))
bigList[num] == False
print ("bigList index {0} == {1}".format(num, bigList[num]))
bigList[num] == True
for multiple in range (2, int(101/num) + 1):
bigList[multiple] = False
return(bigList) #We expect a 'sieve with many holes'
print("the last result of bigList {} ".format(holeofStrainer()))
The wished unrolling of the code
line 11 biglist [False, False, True …...True]
line 13 num 3
line 18 x :2
line 20: 3 divided by 2 is equal to 1.5
biglist index 3 == True
line 13 num 4
line 18 x:2
line 20 4 divided by 2 is equal to 2
biglist index 4 == False
The disapointing output:
line 11 - bigList : [False, False, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True]
the last result of bigList [False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True]
The request.
Where is the obvious lack of understanding in the loop principle?
If that is a tutorial that reffers directly to this kind of error, it is welcome.