728x90
"""
Problem Statement:
Write a program that checks if a given number is an Armstrong number.
An Armstrong number (also known as a narcissistic number) is a number that
is equal to the sum of its own digits each raised to the power of the number of digits.
"""
def is_armstrong_number(num):
num_digits = len(str(num))
sum_of_powers = 0
"""
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))
"""
temp = num
# sum_of_powers += digit ** num_digits
for i in range(1, num_digits+1):
digit = (num % (10 ** (num_digits - i + 1))) // (10 ** (num_digits - i))
sum_of_powers += digit ** num_digits
temp -= digit * (10 ** (num_digits - i))
return sum_of_powers == num
number = int(input("Enter a number: "))
if is_armstrong_number(number):
print(f"{number} is an Armstrong number.")
else:
print(f"{number} is not an Armstrong number.")
728x90
'호그와트' 카테고리의 다른 글
AI 네트워크 방어 게임 (Cyber Shield) 2 by A to Z 영웅 (사이드 사이드 프로젝트) (0) | 2024.07.24 |
---|---|
AI 네트워크 방어 게임 (Cyber Shield) 1 by A to Z 영웅 (사이드 사이드 프로젝트) (0) | 2024.07.24 |
good job (0) | 2024.07.14 |
crazy dfs appeal (0) | 2024.07.13 |
미로르미롤미로를 찾아서 (0) | 2024.07.13 |