[Tutor] Prime numbers

Luke Paireepinart rabidpoobear at gmail.com
Sun Mar 28 10:46:44 CEST 2010


On Sat, Mar 27, 2010 at 5:08 PM, yd <ydmt923 at gmail.com> wrote:

>
> Having a problem finding the first 1000 prime numbers, here is my code:-
>
> print(2)
> n =3
> counter =1
> while counter <=1000:
>   for x in range(3,int((n**0.5)),2):
>     if n%x != 0:
>       print(n)
>       n+=1
>       counter+=1
>     else:
>       n+=1
>
> The problem is, it prints 2 and then does nothing, yet if i try and close,
> it says program is still running do you want to kill it, is there a way to
> do this with lists, i know python has a prime function but i am not going to
> use it because i want to solve this problem without 'cheating'.Thanks.
>
>
> What 'problem' are you trying to solve?
In general, anytime you can use a premade solution, you are at an advantage,
not cheating.
That's one of the marks of a truly good programmer - being able to reuse as
much code as possible.
Unless it's a homework problem and he said "don't use the prime function"
because in this case, your goal is to learn how to write a prime function,
not to calculate primes.
I.E. it's the doing, not the task.  And if that's the case, please let us
know that it's homework.  We will still help you, we just follow certain
guidelines
when providing homework assistance so as not to "give it away" and still
allow you to reason / come up with the solution on your own, as your teacher
probably intended.

-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100328/5136cada/attachment.html>


More information about the Tutor mailing list