Day9: print out all the numbers daffodils
# Narcissistic number: a three-digit number, the digits of its cube is equal to the data itself
# 153 is a number, for example, daffodils, since 153 = 1 ^ 3 ^ 3 + 5 + 3 ^ 3
Method one: first converted to a string digital form, will turn into each list
1 def num(): 2 3 n = 100 4 list_1 = [] 5 while n <= 999: 6 list = [int(i) for i in str(n)] 7 if list[0]**3 + list[1] ** 3 + list[2] ** 3 == n: 8 list_1.append(n) 9 n += 1 10 return list_1 11 12 13 print(num()) 14 15 #或者转换为for循环 16 def num(): 17 list_1 = [] 18 for n in range(100,1000): 19 list = [int(i) for i in str(n)] 20 if list[0]**3 + list[1] ** 3 + list[2] ** 3 == n: 21 list_1.append(n) 22 return list_1 23 24 25 print(num())
Output:
Method Two: The method can find out all such numbers, digit (1634 ^ 4 + 1 = 6 + 3 ^ 4 ^ 4 ^ 4 + 4), and so the number of five
1 def num(): 2 list_1 = [] 3 for i in range(100,1000000): 4 s = 0 5 number = str(i) 6 for j in number: 7 s += int(j) ** len(number) 8 if s == i: 9 list_1.append(i) 10 return list_1 11 12 if __name__ == '__main__': 13 print (whether ())
Output: