[Tutor] RE: Prime Numbers

ahimsa ahimsa@onetel.net.uk
Wed Feb 26 17:45:05 2003


Hello Ike

Thank you for the more detailed explanation - I could follow the majority=
 of=20
the code (I enjoyed the user interactions!!) although what is the=20
'OverFlowError' parameter of the exception? Does that refer to unwieldy f=
loat=20
to integer conversions? Is that a module or a builtin? Sorry, but am show=
ing=20
my crass ignorance here. I appreciate the algorithm about differentiating=
=20
between the evens and odds that 2 is the only prime that is even. That's =
a=20
neat way of figuring out the initial stages of the problem. Not coming fr=
om a=20
math background, this wouldn't have occurred to me to do. Good tip.

I was able to mostly follow what you did but must confess to finding it a=
 bit=20
hard going about the middle of the "if y <=3D 3:" suite not being sure wh=
ere=20
the "primeflag" and "high" variables fit in, and what purpose they serve.=
 My=20
confusion may have also been compounded by the way that your code structu=
re=20
was messed up by the email client. Nevertheless, if you wouldn't mind, co=
uld=20
you kindly go over that step a little slower.

Thank you Ike - this list is a great source of learning!!!

All the best
Andrew

>   for i in range(start,y,2):
>     high=3Dint(math.sqrt(i))   #we only check between 2 and sqrt(i)
>     primeflag=3D1
>     for j in range(2, high):
>        if not i%j:
>              primeflag=3D0
>        else:
>              pass
>     if primeflag:
>         primes.append(i)
>
>   return tuple(primes)