473,760 Members | 8,623 Online
Bytes | Software Development & Data Engineering Community
+ 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 1480
je********@yaho o.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********@yaho o.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(536870 911), 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********@yaho o.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********@yaho o.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
1795
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 as a Wi32 service. When the python.exe process consume ~122MB (according to the Windows Task Manager) I start receiving MemoryError: out of memory exceptions. I've already checked for bad RAM and the server's RAM appears good.
0
1913
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 MB in size. This is on a PC with 500MB of RAM, so memory shouldn't be an issue. Here's the portion of code leading up to the error that is doing the download:
2
2057
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", "credits" or "license" for more information. >>> import parser >>> parser.suite('# -*- coding: IBO-8859-1 -*-')
0
1207
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
1441
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 do or add. (issue 1) When setting up a panel control with border settings: <asp:Panel Backcolor="BurlyWood" Borderstyle="Ridge" BorderColor="Black" BorderWidth="1px" ... Runat="server" />
0
1334
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 exceptions when using the IMAP downloads. This happens randomly; sometimes the file downloads OK, and other times no. Using an exception and traceback function, I can see the following... MemoryError <no args> File "C:\Documents and...
1
2558
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 "./tmveritraceparser.py", line 33, in ? for lineStr in fin.xreadlines(): MemoryError Code snippet is:
3
2333
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: Traceback (most recent call last): File "t2.py", line 70, in <module> walk_dirs(sys.argv) File "t2.py", line 53, in walk_dirs actual = calculate_digest(datafile) File "t2.py", line 28, in calculate_digest
0
1807
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>= 73300 or N<=70400, it works well. Yet, within the range(70400,73300), it goes wrong with a error message "MemoryError". I am very confused about that. My python version is "Python2.4.3", scipy version is "scipy 0.5.0.2033". By the way, my computer's...
0
9333
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10107
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9900
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7324
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6599
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3863
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.