[EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list