noazet abdo
noazet abdo

Reputation: 113

Python memory error in CPU and wall time

I am trying to do a function to calculate the modular exponential of three variables and compare the CPU and wall time when:

e=2^n and e=2^n-1

Here is my code:

from random import choice
import random
def question_3(m,n):
    list = []
    for i in range(2,2**m):
        flag=True
        for num in list:
            if(i%num==0):
                flag=False
        if(flag):
            list.append(i)
            p = choice(list)
            a = randint(1,int(p)-1) 
            e = pow(2,n)
    return pow(a,e,p)
time t = question_3(150,100)

But when I enter m and n with huge numbers, it gives me:

range() result has too many items

Upvotes: 2

Views: 109

Answers (1)

aIKid
aIKid

Reputation: 28352

That's because you're forcing range to generate too much data. For example, range(2, 2*1234567891011) will generate a list with the length of 2*1234567891011-2, isn't it a bit too much?

Try using xrange() instead, it generates the data when needed, instead of making all of the data when you call it.

Change it to this:

for i in xrange(2,2**m):

Looks simple, but it has a huge difference. Hope this helps!

Upvotes: 1

Related Questions