0

My program is to find the number of perfect squares between any two given numbers(both inclusive)

# Enter your code here. Read input from STDIN. Print output to STDOUT
import math
t=int(raw_input())
for i in range(0,t):
    count=0
    s,e=raw_input().strip().split(' ')
    s,e=[int(s),int(e)]
    for j in range(s,e+1):
        if math.sqrt(j)==math.floor(math.sqrt(j)):
            count+=1
    print count

When the input is for example:

3

59 999999922

9 999999966

12 999999988

The error that pops up is:

Traceback (most recent call last): File "solution.py", line 8, in for j in range(s,e+1): MemoryError

Why is this error coming up? and how can I solve it? Sorry I am new to this stackoverflow.

Aman
  • 33
  • 7

1 Answers1

0

range() creates a list in memory and if the value of t gets big enough you are running our of memory. Use lazy evaluation, replace range() with xrange().

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195