Python 101 and Math Logic - Listing square root numbers less than n
I'm stuck on a Python 101 type problem involving loops. Here are the
directions:
The square numbers are the integers of the form K × K, e.g. 9 is a square
number since 3 × 3 = 9. Write a program that reads an integer n from input
and outputs all the positive square numbers less than n, one per line in
increasing order. For example, if the input is 16, then the correct output
would be
1
4
9
This is what I have so far but it sort of works but runs on forever. My
code never reaches the if statement so it breaks(stops) before it gets to
17.
Suppose n = 17.
n=int(input())
counter = 1
while counter * counter < n:
for counter in range(1,n):
a = counter*counter
print(a)
if a < n:
break
Results:
1
4
9
16
25
36
49
64
81
No comments:
Post a Comment