0

So I have been running into a really weird error. I have a module ten.py containing

import math

def go():
    list_ = list(range(3, 100000, 2))
    max_ = int(math.sqrt(len(list_)))
    print(len(list_))
    print(max_)
    for i in range(1,max_):
        print(i)
        current = list_[i]
        for j in list_[i+1:]:
            if j % i == 0:
                list_.remove(j)
go()

The output is this:

49999
223
1
2
Traceback (most recent call last):
  File "D:\Documents\KomodoProjects\Project Euler\ten.py", line 14, in <module>
    go()
  File "D:\Documents\KomodoProjects\Project Euler\ten.py", line 10, in go
    current = list_[i]
IndexError: list index out of range

As you can see the size of list is 49999 and the for loop only goes to 223. Despite all this it already gives an index out of range exception at index=2!

Is the list_ inside the for loop somehow not referencing to the list_ inside go()? I have no clue as to why this problem occurs.

Fixed by changing

for i in range(1,max_):

to for i in list_:

Daan Lubbers
  • 15
  • 1
  • 8
  • The traceback mentions a variable `current` yet this variable does not appear in your given code. Maybe you should post the actual code you are running. – steveha Feb 20 '13 at 20:19
  • Oh sorry I removed that line but Didn't update the traceback. Editing now. – Daan Lubbers Feb 20 '13 at 20:19
  • Also - for Euler in general - you may find this post of interest http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python – Jon Clements Feb 20 '13 at 20:19
  • Thanks, I think I skimmed over that one earlier but for now I am more curious as to why this specific error occurs. – Daan Lubbers Feb 20 '13 at 20:22
  • Hint: `i` is 1 to start with, right? Which numbers are congruent to 0 mod 1, and will therefore be deleted if you loop over them? – DSM Feb 20 '13 at 20:25
  • Thanks the answer helped me fixed it. list_ = list(range(3, 2000000, 2)) takes forever though, which is what I want to do for the Euler problem. Any idea why? Is there a faster way? – Daan Lubbers Feb 20 '13 at 20:35

1 Answers1

0

Here is your problem:

You have a for loop that is deleting numbers from list_. It is deleting so many numbers that list_ is length 2, and then list_[2] fails and raises an exception.

The reason it is deleting so many numbers is that you are computing x % 1 == 0 which is true for any value of x. Since you start at position 2 in the list, you are deleting everything after that position.

I'm not certain exactly what you are doing but this is going to be a slow way to do it, whatever it is. This looks sort of like screening for prime numbers... you might want to do a Google search for "Python find prime numbers" or something.

steveha
  • 67,444
  • 18
  • 86
  • 112
  • 1
    Particularly, at `i=1`, *any* value of `j` will return `true` for `j%i==0` (1 divides evenly into everything). – Hannele Feb 20 '13 at 20:27
  • Oh wow somehow the outer for loop started at 1. It was supposed to not even be a range() but just list_. That must have gotten messed up somehow while I was testing different things. – Daan Lubbers Feb 20 '13 at 20:29