Test if a number is equal to the sum of the cubes of its digits. Find the smallest and largest such numbers.
Test if a number is equal to the sum of the cubes of its digits.
n=input(" Enter a number ")
sum=0
for i in range(0,len(n)):
sum=sum+pow(int(n[i]),3)
print("Sum of cube of digits : ",sum)
if(sum==int(n)):
print("Digits are matching to cube : ", n)
else:
print("Digits are NOT matching to cube : ", n)
Output
Enter a number 371
Sum of cube of digits : 371
Digits are matching to cube : 371
Find the smallest and largest such numbers.
my_list=[]
for n in range(10,100000): # increase this range
sum=0
my_str=str(n)
k=len(my_str)
for i in range(0,k):
sum=sum+pow(int(my_str[i]),3)
if(sum==int(n)):
print("This is an matching number: ",n)
my_list.append(n)
print("Highest number : ",max(my_list))
print("Lowest number : ",min(my_list))
Output
This is an matching number: 153
This is an matching number: 370
This is an matching number: 371
This is an matching number: 407
Highest number : 407
Lowest number : 153