[Python-bugs-list] [ python-Bugs-469859 ] range function problem

noreply@sourceforge.net noreply@sourceforge.net
Wed, 10 Oct 2001 06:12:51 -0700


Bugs item #469859, was opened at 2001-10-10 06:08
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=469859&group_id=5470

Category: Python Library
>Group: Not a Bug
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: zhaoyun (jilly)
Assigned to: Nobody/Anonymous (nobody)
Summary: range function problem

Initial Comment:
I am a new user of python language.
I tested a small program which likes the following:
********************************************
*  n = 19999999 
*  for x in range(2, n/2+1):
*       if n % x == 0:
*            print n, 'equals', x, '*', n/x
*            break
*  else:
*       print n, 'is a prime number'
**********************************************
I found something interesting when the variable 'n' 
equals 19999999 
that my computer seemed crazy because it began to eat 
my memory
at a surprising speed ,the memory amount used just 
rose from 131M to 258M 
and the process had no response. 
 
But when the variable 'n' equals 1999999, the program 
works well.
 
So I just ran the expression 'range(2, 
n/2+1)'(n=19999999),and i found
the same situation like the front happened.
 
So I want to know if it is a bug of the range function.
 


----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-10-10 06:12

Message:
Logged In: YES 
user_id=6380

It's not a bug -- the range() function creates a list
containing all those numbers, and that takes up a lot of
memory.

If you want to loop over a large range without consuming so
much memory, use xrange() instead of range().

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=469859&group_id=5470