18. Write a program to input a list of numbers and test if a number is equal to the sum of the cubes of its digits.
Find the smallest and largest such number from the given list of numbers.
Solution
Answers
A number that is equal to the sum of cubes of its digits is called an Armstrong number.
# A function to check Armstrong number
def is_armstrong(number):
number_of_digits = len(str(number))
temp = number
sum_of_digits = 0
while temp != 0:
digit = temp % 10
sum_of_digits += pow(digit, number_of_digits)
temp //= 10
return sum_of_digits == number
# Getting a sorted list of numbers
numbers = sorted([int(number) for number in input("Enter the numbers - ").split( )])
# Finding largest armstrong number
for largest in numbers[::-1]:
if is_armstrong(largest):
print("Largest Armstrong number -", largest)
break
# Finding smallest armstrong number
for smallest in numbers:
if is_armstrong(smallest):
print("Smallest Armstrong number -", smallest)
break