For loop method:
h = 19
x = []
q = 0
for i in range(1,h+1):
if i**2 < h:
q +=1
x.append(i**2)
List comprehension:
squares = [i**2 for i in range(1,h+1) if i**2 < h]
q = len(squares)
Richard P.
asked 03/30/24Write the code to count the number of perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Assign the sum you compute to a variable q. For example, if h is 19, you would assign 4 to q because there are perfect squares (starting with 1) that are less than h are: 1, 4, 9, 16.
For loop method:
h = 19
x = []
q = 0
for i in range(1,h+1):
if i**2 < h:
q +=1
x.append(i**2)
List comprehension:
squares = [i**2 for i in range(1,h+1) if i**2 < h]
q = len(squares)
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.