I
I
its_a_me_mario2020-12-17 15:23:14
Mathematics
its_a_me_mario, 2020-12-17 15:23:14

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

3 answer(s)
S
Sand, 2020-12-17
@its_a_me_mario

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

A
Andrey Dugin, 2020-12-17
@adugin

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')))

E
ertbn, 2021-02-24
@ertbn

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)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question