Answer to Question #82643 in Python for Karen

Question #82643
Assume there are two variables, k and m, each already associated with a positive integer value and further assume that k's value is smaller than m's. Write the code necessary to compute the number of perfect squares between k and m. (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).) Associate the number you compute with the variable q. For example, if k and m had the values 10 and 40 respectively, you would assign 3 to q because between 10 and 40 there are these perfect squares: 16, 25, and 36. Do NOT modify k and m.
This is what I have but I keep getting that q is incorrect
i=1
q=0
while i*i<k:
i+=1
while i*i<=m:
q+=1
i+=1
1
Expert's answer
2018-11-05T10:00:09-0500

k = int(input())

m = int(input())

q = 0

sqrtk = int(k ** 0.5)

sqrtk += 1

sqrtm = int(m ** 0.5)

if sqrtm ** 2 != m:

sqrtm += 1

for i in range(sqrtk, sqrtm):

q += 1


print(q)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS