6.1. While: List of squares
pole55 (233)

"For a given integer N, print all the squares of positive integers where the square is less than or equal to N, in ascending order."

Example input

Example output
1 4 9 16 25 36 49

You are viewing a single comment. View All
girianshiido (4)

@pole55 what I meant is that your script is computing too much stuff for its purpose. That's because range(1, x+1) is way too big, especially if x itself is big. If x=10^10, then you only really need to stop at 10^5, because the square of this number is 10^10.
Here is a modification of your own script:

from math import sqrt
x = int(input("Enter the number: "))
print("All the squares in the given range are:")
print([i**2 for i in range(1, int(sqrt(x))+1) if i**2 <= x])

Try your script with x = 10000000000 and try this new version. You'll see the difference.