# 6.1. While: List of squares

"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

50

Example output

1 4 9 16 25 36 49

PokmanDeng1

x = int(input())

i=int(0)

q=str()

while(1==1):

i=i+1;

if(i**2>=x):
break
q=q+str(i**2)+" "

print(q)

JindongSha

THANKS

I think it's better to use a while loop here.

For example, if x is one million, the last number of the list will be one million, but you'll compute the square of each number from one to one million (so the last square will be one trillion).

range(1, x+1)is way too big, especially ifxitself is big. Ifx=10^10, then you only really need to stop at10^5, because the square of this number is10^10.Here is a modification of your own script:

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

its all because of "if i**2 <= x" that tells to stop at "10^5" (in your words). without this part, it will print "10^10".

Your code doesn't tell to stop at 10^5. It'll try to compute each square from 1 to x², even if i² is already greater than x. Of course, none of these numbers will be in the list, because they don't verify the condition.

By the way, I just realized I forgot to remove the unnecessary condition

if i^2 <=x.