[Tutor] prime test problem

Roelof Wobben rwobben at hotmail.com
Sat Aug 21 19:30:57 CEST 2010


Hello, 

 

 

Thank you.

It worked now.

 

x=x+1 must have the same indention als the if then and the return.

 

Roelof


 
> Subject: Re: [Tutor] prime test problem
> From: evert.rol at gmail.com
> Date: Sat, 21 Aug 2010 19:24:51 +0200
> CC: tutor at python.org
> To: rwobben at hotmail.com
> 
> > Hello,
> > 
> > I tried it with this simple programm 
> > 
> > def is_prime(n):
> > x=2 
> > 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.
> 
> 
> > x=x+1;
> 
> 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
> > 
> > x=is_prime(7)
> > 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 ;-).
> 
> 
> Evert
> 
> 
> 
> > 
> > Roelof
> > 
> > 
> > > 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:
> > http://mail.python.org/mailman/listinfo/tutor
> 
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100821/73341f03/attachment.html>


More information about the Tutor mailing list