[Tutor] Strange modulus problem
Andrew Wilkins
toodles@yifan.net
Fri, 12 Oct 2001 11:35:41 +0800
I think what I have to say is right, if I'm not someone please correct me :)
> I'm working on a bit of code to determine all of the factors up to and
> including a number entered by the user. Here's my code:
>
> #!/usr/bin/python
>
> import string
>
> num=raw_input("Enter a number: ")
> num_int=string.atoi(num)
>
> factor_range= range(1, num_int + 1)
>
> for item in factor_range:
> if num_int % item > 0:
> factor_range.remove(item)
"for" goes through the indices 0..len(factor_range)
As you remove each item it decreases the size of factor_range, and the index
that for points to next will contain something else.
Example:
>>> test_range=range(10)
>>> for i in test_range:
print i,test_range
test_range.remove(i)
0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2 [1, 2, 3, 4, 5, 6, 7, 8, 9]
4 [1, 3, 4, 5, 6, 7, 8, 9]
6 [1, 3, 5, 6, 7, 8, 9]
8 [1, 3, 5, 7, 8, 9]
So as you can see in the 2nd iteration it is pointing to index 1 as it
should be, but this now points to "2".
So to fix this you could do it one of these (untested) ways:
1) Preserve what you've done already
factor_range=range(1,num_int+1)
loop_range=factor_range[:] #copy contents
for item in loop_range:
if num_int % item > 0:
loop_range.remove(item)
2) Do it the way I prefer to do it :)
factors=[]
for i in range(1,num_int+1):
if num_int%i==0: factors.append(i)
...I'm sure there's still more ways.
> Why doesn't '5' fail the if test and get removed? If I modify the code so
> that it prints the modulus of each item, 5 correctly evalutates to '1'.
What
> gives?
>>> fr=range(1,7)
>>> for item in fr:
print item,fr
if 6%item>0: fr.remove(item)
1 [1, 2, 3, 4, 5, 6]
2 [1, 2, 3, 4, 5, 6]
3 [1, 2, 3, 4, 5, 6]
4 [1, 2, 3, 4, 5, 6]
6 [1, 2, 3, 5, 6]
It's not even looking at 5 :)
It gets to "4" and removes it, thus bringing the next index to point to "6".
HTH
Andrew
>
> - Chris
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>