Answer the question
In order to leave comments, you need to log in
How to count the number of numbers in a given interval whose cube ends in 4 or 9?
The program receives two integers a and b ( a ≤ b ) as input. Write a program that counts the number of numbers between a and b inclusive whose cube ends in 4 or 9.
a = int(input())
b = int(input())
c = 0
if b > a:
for i in range(a, b + 1):
if (i**3) % 4 == 0 or (i**3) % 9 == 0:
c += 1
elif b == a:
if (a**3) % 4 == 0 or (a**3) % 9 == 0:
c += 1
print(c)
Answer the question
In order to leave comments, you need to log in
Divisibility by 4 or 9 does not mean that the number ends with 4 or 9.
Take the remainder of the division by 10 and compare with 4 and 9
The solution "in the forehead", which is enough to complete the task:
sum(1 for x in range(a, b + 1) if x ** 3 % 10 in {4, 9})
sum(1 for x in range(a, b + 1) if str(x ** 3).endswith(('4', '9')))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question