[Tutor] prime test problem
evert.rol at gmail.com
Sat Aug 21 19:24:51 CEST 2010
> I tried it with this simple programm
> def is_prime(n):
> while x <= int(n**0.5)+1:
> if n % x == 0:
> return False
If your while condition is True, you get into the loop.
Inside the loop, however, you never change anything that could change the condition (ie, neither x nor n get changed).
So the while condition stays the same, ie, True, and you loop indefinitely.
Ah, but here you actually change something so that the while condition could become False. Sadly, this statement is outside the loop (hint hint).
> return True
> if x==True:
> print 7, "is een prime getal"
> else :
> print 7, "is geen prime getal"
> But this one gets in a indefinitive loop.
Try to mentally follow the logic of the loop.
Or use print statements and some small, easily checkable, numbers (prime and non-prime) to see what happens.
Print statements (functions, if you're on Python >= 3) are a simple, but at times extremely quick and useful way to debug scripts.
Ah yes, and either use "is een priemgetal" or "is a prime number", not a mix of both (sorry, couldn't help myself ;-).
> > From: steve at pearwood.info
> > To: tutor at python.org
> > Date: Sun, 22 Aug 2010 02:49:26 +1000
> > Subject: Re: [Tutor] prime test problem
> > On Sun, 22 Aug 2010 12:41:03 am Roelof Wobben wrote:
> > > Hello,
> > >
> > > I know.
> > > I have read them all I believe
> > You've read all 64 million pages that Google finds? Wow, you must be a
> > fast reader! Well done!
> > > but I can't see how I can convert the
> > > algebra to a working programm.
> > Have you *tried*?
> > Perhaps you should try something a little bit less ambitious. Write a
> > program to test whether a number is divisible by 3. Then write a
> > program to test whether a number is divisible by 3 or 5. Then write a
> > third program to test whether a number is divisible by 3, 5 or 7.
> > Then generalise that third program.
> > Off you go. Come back when you have some code. Even if it isn't working
> > code, at least try something.
> > --
> > Steven D'Aprano
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
More information about the Tutor