Weird MemoryError issue

Discussion in 'Python' started by jedi200581@yahoo.co.uk, Jul 19, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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
     
    Diez B. Roggisch, Jul 19, 2006
    #2
    1. Advertising

  3. John Machin Guest

    On 20/07/2006 1:58 AM, 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
     
    John Machin, Jul 19, 2006
    #3
  4. John Machin Guest

    On 20/07/2006 6:05 AM, John Machin wrote:
    > On 20/07/2006 1:58 AM, wrote:
    >> def is_prime n:

    >
    > Syntax error. Should be:
    > def is_prime n:


    Whoops! Take 2:

    Should be:

    def is_prime(n):
     
    John Machin, Jul 19, 2006
    #4
  5. Guest

    John Machin wrote:
    > On 20/07/2006 6:05 AM, John Machin wrote:
    > > On 20/07/2006 1:58 AM, 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, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Rechenberg
    Replies:
    0
    Views:
    491
    Andy Rechenberg
    Aug 27, 2003
  2. Jamie
    Replies:
    0
    Views:
    391
    Jamie
    Nov 27, 2003
  3. Sylvain Thenault
    Replies:
    2
    Views:
    493
    Sylvain Thenault
    Jan 18, 2005
  4. Bernhard Reimar Hoefle

    MemoryError Numarray

    Bernhard Reimar Hoefle, Sep 26, 2005, in forum: Python
    Replies:
    0
    Views:
    378
    Bernhard Reimar Hoefle
    Sep 26, 2005
  5. Stephen G
    Replies:
    0
    Views:
    267
    Stephen G
    Oct 18, 2006
Loading...

Share This Page