basic python question about for loop

J

jmDesktop

From the Python.org tutorial:
.... for x in range(2, n):
.... if n % x == 0:
.... print n, 'equals', x, '*', n/x
.... break
.... else:
.... # loop fell through without finding a factor
.... print n, 'is a prime number'
....
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3



first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through?

http://www.python.org/doc/current/tut/node6.html#SECTION006300000000000000000

Thanks.
 
D

Diez B. Roggisch

jmDesktop said:
From the Python.org tutorial:

... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3



first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through?

print out what range(2, n) for n == 2 is.

And if you didn't know - 2 *IS* a prime.


Diez
 
R

Reedick, Andrew

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of jmDesktop
Sent: Wednesday, April 09, 2008 4:51 PM
To: (e-mail address removed)
Subject: basic python question about for loop
From the Python.org tutorial:
... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3



first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through?

a) 2 is prime, so nothing is wrong.

b) Range isn't doing what you think it's doing:
print range(2,2) []
print range(2,3) [2]
print range(2,4) [2, 3]
print range(2,5)
[2, 3, 4]
print range(1,1) []
print range(1,2) [1]
print range(1,3) [1, 2]



*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622
 
J

jmDesktop

jmDesktop schrieb:







print out what range(2, n) for n == 2 is.

And if you didn't know - 2 *IS* a prime.

Diez- Hide quoted text -

- Show quoted text -

I do not understand.
 
J

jmDesktop

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of jmDesktop
Sent: Wednesday, April 09, 2008 4:51 PM
To: (e-mail address removed)
Subject: basic python question about for loop
From the Python.org tutorial:
for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...             print n, 'equals', x, '*', n/x
...             break
...     else:
...         # loop fell through without finding a factor
...         print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through?

a) 2 is prime, so nothing is wrong.

b) Range isn't doing what you think it's doing:
print range(2,2) []
print range(2,3) [2]
print range(2,4) [2, 3]
print range(2,5)

[2, 3, 4]


print range(1,1) []
print range(1,2) [1]
print range(1,3)
[1, 2]

*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers.. GA622- Hide quoted text -

- Show quoted text -

So what is n and x in the first iteration? Sorry. I'm trying.
 
S

Steve Holden

jmDesktop said:
From the Python.org tutorial:
... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3



first time 2 mod 2, 2/2, no remainder == 0, what am I doing wrong?
Why did it fall through?
>>> range(2, 2) []
>>>

The loop body executes zero times.

regards
Steve
 
R

Reedick, Andrew

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of jmDesktop
Sent: Wednesday, April 09, 2008 5:04 PM
To: (e-mail address removed)
Subject: Re: basic python question about for loop


So what is n and x in the first iteration? Sorry. I'm trying.


You're never getting to n and x in the first iteration, because the 'for x in range(2, n)' loop isn't looping.

This:
for x in range(2, n)
is equivalent in C/Perl/etc. to:
for(x=2; x<n; x++)
which for the first iteration is:
for(x=2; x<2; x++)

Since (2 < 2) is false, you never call 'if n %x == 0:' in the first iteration.

Or to put it another way:
Range(2, n) starts at 2, and stops _before_ n.
Range(2, n) starts at 2, and stops _before_ 2.




*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622
 
D

Diez B. Roggisch

jmDesktop said:
I do not understand.

for <variable> in <sequence> loops over a sequence. And of course it
doens't if the sequence is empty, because you can't loop over something
that is empty, can't you?

and range(2,2) is the empty sequence.

Diez
 
T

Terry Reedy

|So what is n and x in the first iteration? Sorry. I'm trying.

When n == 2, the inner loop executes 0 times (the length of range(2,n)) and
then falls thru to the else clause, printing the correct answer.
 
S

Steve Holden

jmDesktop wrote:
[...]
So what is n and x in the first iteration? Sorry. I'm trying.

Somewhat feebly, if you don't mind my saying so, but don't worry.

The usual way to proceed in the face of such ignorance is to insert some
form of output that will tell you the answer to your question.

So:
.... print range(2, n)
.... for x in range(2, n):
.... if n % x == 0:
.... print n, 'equals', x, '*', n/x
.... break
.... else:
.... print n, "is prime"
....
[]
2 is prime
[2]
3 is prime
[2, 3]
4 equals 2 * 2
[2, 3, 4]
5 is prime
[2, 3, 4, 5]
6 equals 2 * 3
[2, 3, 4, 5, 6]
7 is prime
[2, 3, 4, 5, 6, 7]
8 equals 2 * 4
[2, 3, 4, 5, 6, 7, 8]
9 equals 3 * 3

and so on! This is the value of the interactive interpreter.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top