Narcissistic number , That is, the sum of the cubes of the numbers in each digit is equal to its own number .
example :153
1^3=1
5^3=27
3^3=125
1+125+27
=126+27
=153
= The original number
therefore ,153 It's a narcissus number .
-------------------------------------------
Code
First :
#coding=utf-8
a=str(input(" Numbers :"))
Get the number entered by the user , And turn it into numbers , Easy to traverse
secondly :
s=0
for i in a:
s+=int(i)**3
Define a sum variable , Traversal string , Add the cube of each number
if s==int(a):
print(" yes ")
else:
print(" No ")
Finally, judge whether it is the same , As the same, the output is , Different output is not
Complete code
#coding=utf-8
a=str(input("input:"))
s=0
for i in a:
s+=int(i)**3
if s==int(a):
print(" yes ")
else:
print(" No ")
The way 【2】:
#coding=utf-8
a=int(input("number:"))
Get input number , And convert it to an integer
s=0
v=int(a) # Backup
First define the variables and as 0,a Define a backup ( Can't operate directly a, Otherwise, it cannot be compared )
for i in range(len(str(a))):
last=v%10
s+=last**3
v-=last
v/=10
Traverse by digits a Variable , Use the remainder to calculate the last digit , Add to the cube s, Then remove the last bit
notes :% It means to take the remainder , use v Subtract the remainder , Divided by 10, Equivalent to direct “ Wipe off ” Last digit
Finally, the comparison : Judge whether it is the same as the original number
if s==int(a):
print(" yes ")
else:
print(" No ")
complete :
#coding=utf-8
a=int(input("number:"))
s=0
v=int(a) # Backup
for i in range(len(str(a))):
last=v%10
s+=last**3
v-=last
v/=10
if s==int(a):
print(" yes ")
else:
print(" No ")
-------------------------------------- End ----------------------------------------------------------------------------------------