2

I run this short program in python, but it outputs memory error. I use Sublime Text. My memory usage as I saw in System moniter was just normal, I had more than 2gb memory left.

def is_Prime(p):
    d=int(math.sqrt(p))
    if (p**2)% 12 == 1:
        if p==1:
            return 0
        for i in range(7, d+1, 6):
            if p%i==0:
            return 0
        for i in range(5, d+1, 6):
            if p%i==0:
                return 0    
        return 1
    else:
        if p==2 or p==3:
            return 1
        return 0

is_Prime(2425967623052370772757)
Debdut
  • 129
  • 1
  • 8
  • 1
    Are you using Python 2? If so, you are generating ranges that are approximately 8,209,018,791 items long. Using `xrange()` instead might help. – Frédéric Hamidi May 26 '14 at 13:06
  • 1
    Well... `range(7, d+1, 6)` is generating a **BIG** list in Python 2... – Maxime Lorant May 26 '14 at 13:06
  • If you are using 32bit Python, memory error means you used over 4GB in the Python interpreter. In this case I think `range(d)` will create list which is over 4GB. When it comes to fail creating list, the memory space of the list will be released. Probability by using System monitor you can see increasing memory usage and sudden decreasing. – Kei Minagawa May 26 '14 at 13:48

1 Answers1

3

In this particular case, you could get rid of the range invocations that allocate a list, in favor of xrange that doesn't.

otus
  • 5,572
  • 1
  • 34
  • 48