Re: Problem with following python code

2007-06-11 Thread Tim Leslie
On 6/12/07, why? [EMAIL PROTECTED] wrote:
 I've been having problem with the following code. It's supposed to
 print the prime numbers  between 10 and 100. But i'm not getting any
 output, i.e. i guess the outer 'for' loop is being traversed only
 once. I would be greatful if you could help me out. Thanx!
  f=1
  for i in range(10,100):

You need to switch these two lines to reset the flag each time around
the outer loop.

Cheers,

Tim

 ... for j in range(2,i):
 ... if i%j==0:
 ... f=0
 ... break
 ... else: continue
 ... if f==1:
 ... print i,
 ...

 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with following python code

2007-06-11 Thread Gabriel Genellina
En Tue, 12 Jun 2007 01:25:31 -0300, why? [EMAIL PROTECTED] escribió:

 I've been having problem with the following code. It's supposed to
 print the prime numbers  between 10 and 100. But i'm not getting any
 output, i.e. i guess the outer 'for' loop is being traversed only
 once. I would be greatful if you could help me out. Thanx!
 f=1
 for i in range(10,100):
 ... for j in range(2,i):
 ... if i%j==0:
 ... f=0
 ... break
 ... else: continue
 ... if f==1:
 ... print i,
 ...


Note that once you set f=0, it will never change.
Move the f=1 inside the outer loop. Also, the else clause is useless here;  
best to use a bool for conditions; and it would benefit from better  
variable names. Keeping the same structure:

for number in range(10,100):
 is_prime = True
 for divisor in range(2,number):
 if number % divisor == 0:
 is_prime = False
 break
 if is_prime:
 print number,

Next step: for loops have an optional else clause, that gets executed  
whenever the loop exits normally (in this case, when divisor goes up to  
number, and the break statement is never executed). So you don't need  
is_prime:

for number in range(10,100):
 for divisor in range(2,number):
 if number % divisor == 0:
 break
 else:
 print number,

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with following python code

2007-06-11 Thread Dan Hipschman
On Tue, Jun 12, 2007 at 04:25:31AM -, why? wrote:
 I've been having problem with the following code. It's supposed to
 print the prime numbers  between 10 and 100. But i'm not getting any
 output, i.e. i guess the outer 'for' loop is being traversed only
 once. I would be greatful if you could help me out. Thanx!
  f=1
  for i in range(10,100):
 ... for j in range(2,i):
 ... if i%j==0:
 ... f=0
 ... break
 ... else: continue
 ... if f==1:
 ... print i,
 ...

Move f=1 inside the outer loop:

for i in range(10,100):
f=1
for j in range(2,i):
if i%j==0:
f=0
break
else: continue
if f==1:
print i,

It gets set to 0 in the first iteration and never has another chance to
be set to 1 after that.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with following python code

2007-06-11 Thread Dick Moores
At 09:52 PM 6/11/2007, Dan Hipschman wrote:
On Tue, Jun 12, 2007 at 04:25:31AM -, why? wrote:
  I've been having problem with the following code. It's supposed to
  print the prime numbers  between 10 and 100. But i'm not getting any
  output, i.e. i guess the outer 'for' loop is being traversed only
  once. I would be greatful if you could help me out. Thanx!
   f=1
   for i in range(10,100):
  ... for j in range(2,i):
  ... if i%j==0:
  ... f=0
  ... break
  ... else: continue
  ... if f==1:
  ... print i,
  ...

Move f=1 inside the outer loop:

for i in range(10,100):
 f=1
 for j in range(2,i):
 if i%j==0:
 f=0
 break
 else: continue
 if f==1:
 print i,

It gets set to 0 in the first iteration and never has another chance to
be set to 1 after that.

And of course the inner loop does too much work. Try:

for i in range(10,100):
 f=1
 max = int(i**.5 + 1)
 for j in range(2,max):
 if i%j==0:
 f=0
 break
 else: continue
 if f==1:
 print i,

Dick Moores

-- 
http://mail.python.org/mailman/listinfo/python-list