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)
3
  • 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. Commented May 26, 2014 at 13:06
  • 1
    Well... range(7, d+1, 6) is generating a BIG list in Python 2... Commented May 26, 2014 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. Commented May 26, 2014 at 13:48

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.