[Tutor] Sieve of Erastthotenes without sofisticated tools

Dave Angel davea at davea.name
Tue May 5 07:14:44 CEST 2015


You accidentally sent your response to me instead of the list.  The 
proper thing to do for nearly all messages is to respond to the list. 
The main exception for that is if you just want to leave a simple 
thank-you for a person, and nothing of interest to anyone else.

 From your email program, use Reply-List.  If it doesn't support that, 
use Reply-All, and then delete me, so it goes just to the list.

Another problem with your message is messed-up formatting, apparently 
caused by the fact that you're sending html-encoded email to a text-only 
mailing list.  Please tell your mailer to use text.

On 05/04/2015 10:07 PM, yvan moses levy wrote:> Le 04/05/15 13:00, Dave 
Angel a écrit :
 >> On 05/04/2015 03:19 AM, yvan moses Levy wrote:
 >>> My code is wrong!
 >>
 >> You'd find it a lot easier to get responses if you'd say in what way 
the code
 >> is wrong.  If you get an exception, show the full traceback.  If you get
 >> printed results, show what you expected, and what you got instead. 
If it
 >> hung, or crashed the OS, or ran out of memory, say so.
 >>
 >>> I tried and tried
 >>> But I'm very isolated and It's hard without consultation with a tutor
 >>> <code>from math import sqrt
 >>> def holeofStrainer():
 >>>    bigList = [False, False] + [True]*100
 >>>    print("line 4 - bigList : ", bigList)
 >>>    for num in range(2, 101):
 >>>      print("line 6 - num : ", num)
 >>>      for x in range(bigList[2], bigList[int(sqrt(num)) + 1]):
 >>
 >> What did you expect this to do?  What is bigList[2] ?  What is
 >> bigList[int(sqrt(num)) + 1] ?  Are these reasonable values to put into a
 >> range() function?
 >>

You didn't answer any of these questions.  Why not?

 >>
 >>
 >>>        print("line 8 x : %d"%x)
 >>>        if num % x == 0:
 >>>          print("line 10 {0} divise par {1} = {2} ".format(num, x, 
num/x))
 >>>          bigList[num] == False
 >>>          print "bigList[{0} == {1}]".format(num, bigList[num])
 >>>        bigList[num] == True
 >>>
 >>>      for multiple in range (2, int(101/num) + 1):
 >>>        bigList[multiple] = False
 >>>    return(bigList)
 >>> print("the last result of bigList {} ".format(holeofStrainer()))</code>
 >>> I WANT TO KNOW WHILE THE EXECUTION DO NOT GOING DOWNWARD
 >>>
 >>>
 >> OK I'll trying
 > code:
 > <code>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]):

I repeat, what did you expect this line to do? Examine this line 
carefully.  Print out the two expressions you're passing to range().

This currently loops from True to True, or from False to False, so it 
skips the whole predicate. Think about what you really want the loop to do.


 >         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()))</code>*
 >
 > *
 >
 > ***Hi Dave.*

What's the reason you're quad-spacing the expected output?  I'm fixing 
it, but it's a pain to do.

 >
 > **
 > > *I expected to obtain an output as:*
 >
 > *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*

So that's what you expected.  What did you get instead?  If you notice 
that line 18 never printed, shouldn't you guess that something's wrong 
with the loop in line 17?



-- 
DaveA


More information about the Tutor mailing list