473,463 Members | 1,429 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Weird MemoryError issue

Hi,

I'm new at python as I just started to learn it, but I found out
something weird. I have wrote a little program to compute Mersenne
number:

# Snipet on
def is_prime n:
for i in range(2, n):
if (n % i) == 0:
return 0
else:
return 1

for a in range(2, 10000000):
if (is_prime(a) and is_prime(2**a-1))
print 2**a-1, " is a prime number"
# Snipet off

This program raise MemoryError. But this one:

# Snipet on
def is_prime n:
for i in range(2, n):
if (n % i) == 0:
return 0
return 1 # the change is here

for a in range(2, 10000000):
if (is_prime(a) and is_prime(2**a-1))
print 2**a-1, " is a prime number"
# Snipet off

Does not! Why ??

Thx.

Jul 19 '06 #1
4 1469
je********@yahoo.co.uk wrote:
Hi,

I'm new at python as I just started to learn it, but I found out
something weird. I have wrote a little program to compute Mersenne
number:

# Snipet on
def is_prime n:
for i in range(2, n):
if (n % i) == 0:
return 0
else:
return 1

for a in range(2, 10000000):
if (is_prime(a) and is_prime(2**a-1))
print 2**a-1, " is a prime number"
# Snipet off

This program raise MemoryError. But this one:

# Snipet on
def is_prime n:
for i in range(2, n):
if (n % i) == 0:
return 0
return 1 # the change is here

for a in range(2, 10000000):
if (is_prime(a) and is_prime(2**a-1))
print 2**a-1, " is a prime number"
# Snipet off

Does not! Why ??

Don't use range, use xrange. The former will generate an actual list in
memory, which you usually don't need - especially in for-loops as you
don't even have a reference on the list itself.

And I don't think that your change afflicts memory consumption at all. Must
be a coincidence.

Diez
Jul 19 '06 #2
On 20/07/2006 1:58 AM, je********@yahoo.co.uk wrote:
Hi,

I'm new at python as I just started to learn it, but I found out
something weird. I have wrote a little program to compute Mersenne
number:

# Snipet on
def is_prime n:
Syntax error. Should be:
def is_prime n:
for i in range(2, n):
if (n % i) == 0:
return 0
else:
return 1

for a in range(2, 10000000):
if (is_prime(a) and is_prime(2**a-1))
Syntax error (missing :)
print 2**a-1, " is a prime number"
# Snipet off

This program raise MemoryError. But this one:

# Snipet on
def is_prime n:
for i in range(2, n):
if (n % i) == 0:
return 0
return 1 # the change is here

for a in range(2, 10000000):
if (is_prime(a) and is_prime(2**a-1))
print 2**a-1, " is a prime number"
# Snipet off

Does not! Why ??
Neither of the above compiles without error. You may regard this as a
novel suggestion (it's not), but try pasting the actual code that you ran.

When corrected, the versions are functionally equivalent. They give the
same result: on my box, they fell over trying to do
is_prime(536870911), in particular trying to do range(536870911). If
successful, that would produce a list of approx 0.5G elements. On a
32-bit box, that's about 2GB of pointers to objects. Each object has a
reference count, a pointer to its type, and its value -- another 6GB.
Total about 8GB. Use xrange() to avoid the memory grab.

HTH,
John
Jul 19 '06 #3
On 20/07/2006 6:05 AM, John Machin wrote:
On 20/07/2006 1:58 AM, je********@yahoo.co.uk wrote:
>def is_prime n:

Syntax error. Should be:
def is_prime n:
Whoops! Take 2:

Should be:

def is_prime(n):

Jul 19 '06 #4

John Machin wrote:
On 20/07/2006 6:05 AM, John Machin wrote:
On 20/07/2006 1:58 AM, je********@yahoo.co.uk wrote:
def is_prime n:
Syntax error. Should be:
def is_prime n:

Whoops! Take 2:

Should be:

def is_prime(n):
Sorry for that, I was not able to cut-paste the code at the moment, and
was in a hurry, I know it's bad.

Thanks for "xrange", I tried and it works fine.

Jul 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andy Rechenberg | last post by:
The problem is basically in the subject line. I am building an Intranet site based on Zope/CMF/Plone. In order to fulfill the requirements Zope/Python must be run on Windows. Zope is running...
0
by: Jamie | last post by:
Hi all, I have a Python 2.3.2 application that uses the httplib package to download files. On Windows XP only, I'm getting a MemoryError exception in the socket module when files are around 2.8...
2
by: Sylvain Thenault | last post by:
Hi there ! I've noticed the following problem with python >= 2.3 (actually 2.3.4 and 2.4): syt@musca:test$ python Python 2.3.4 (#2, Sep 24 2004, 08:39:09) on linux2 Type "help", "copyright",...
0
by: Bernhard Reimar Hoefle | last post by:
I have the following python script: ####################################### from numarray import * while 1: a=arange(1,300000000) b=a*100/100 del a del b...
1
by: Mark Denardo | last post by:
I recently upgraded to VS2005 and converted some projects from 1.1 to 2.0 and am seeing two weird behaviors I can't seem to resolve, and am wondering if they are bugs or something I'm forgetting to...
0
by: Stephen G | last post by:
Hi there. I have been receiving MemoryErrors using the Windows version of Python 2.5. The script I have written times the sending and the reception of emails with various attachments. I get many...
1
by: manvi | last post by:
Hi, I have written a small python script to parse a very large data set. It works for sometime and then stops with an error message: Traceback (most recent call last): File...
3
by: mandrews58 | last post by:
Hi there, I have a script which walks a directory and calculates the md5 digest on a bunch of large files (~1G). The first file found works fine, but the second generates a MemoryError: ...
0
by: couge | last post by:
Hi all, I met a weird problem when using scipy.integrate.odeint(). It is followed as: from scipy import * y=integrate.odeint(lambda y,t:sin(y)*y,y0=random.rand(N),t=arange(0,100)) If N>=...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.