호그와트

good job

영웅*^%&$ 2024. 7. 14. 21:55
728x90
"""
Write a program that calculates the digital root of a given number.
The digital root is the single-digit value obtained by an iterative process of summing digits,
on each iteration using the result from the previous iteration to compute a digit sum.
The process continues until a single-digit number is obtained.

For example, the digital root of 456 is calculated as follows:

4 + 5 + 6 = 15
1 + 5 = 6
So, the digital root of 456 is 6.
"""
 
 
 
문제가 진짜 진짜 쉬워보이는데 
생각보다 난이도가 있네요 
아무것도 쓰지 않고 암산으로만 풀어보세요 
아래 코드를 보지 말고 위에 문제만 보시고 스스로 풀어보세용~ 
 
 
 
 
 
def is_less_than_tenth(num):
    return isinstance(num, int) and 0 <= num < 10

x = int(input('Please give me any number: '))
result = 0

while True:
    leng = len(str(x))
    for i in range(1, leng + 1):
        temp = (x % (10 ** (leng - i + 1))) // (10 ** (leng - i))
        result += temp
        x -= temp * (10 ** (leng - i))

    if is_less_than_tenth(result):
        break

    x = result
    result = 0

print('Digital root:', result)
728x90