6.1. While: List of squares
pole55 (235)

"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 I know they’re identical. On my 2011 MBA, it takes forever with your code but it works instantly with the sqrt.
By the way, I just realized I forgot to remove the unnecessary condition if i^2 <=x.

print([i*i for i in range(1, int(sqrt(x))+1)])