Xiaobai Collection | Using Python to calculate the number of daffodils

Insert picture description here
Narcissistic number (Narcissistic number) is also known as pluperfect digital invariant (PPDI), narcissistic number, self-power number, Armstrong number or Armstrong number. Narcissus number refers to A 3 digit number, the sum of the powers of 3 of the digits of it is equal to itself (for example: 1^3 + 5^3+ 3^3 = 153). To judge whether a number is a daffodil number, you must first obtain the numbers in the ones, tens, hundreds..., and then calculate the Nth power of these numbers and add them up.
Overall code:
1# Method 1
2start = 101
3end = 999
4for i in range(start, end + 1):
5 # Calculate the number in the hundreds place
6 bai = i // 100
7 # Calculate the number in the tens place and the ones place Number
8 shi, ge = (i-bai * 100) // 10, i% 10
9 # Determine whether it is the number of daffodils
10 if ge * * len(str(i)) + shi * * len(str(i) ) + bai ** len(str(i)) == i:
11 print(i)

1# Method two
2end = int(input('Please enter the maximum range:'))
3for i in range(1, end + 1):
4 # Calculate the length of the number i
5 length = len(str(i))
6 sm = 0
7 temp = i
8 for j in range(length):
9 # For the remainder of 10 and wait for the numbers in the ones digit,
10 # then calculate the length to the power and add up the sum
11 sm += (temp% 10) * * length
12 # Reduce the target number by 10 times, the next step will get the number
13 on the tens place # and so on, the next time get the number
14 on the hundreds and thousands temp //= 10
15 # Determine whether it is a daffodil Count
16 if sm == i:
17 print(i)
There are many ways to calculate the number of daffodils. You can try different solutions.
Part of the content of the article comes from the Internet, contact intrusion and deletion* The
article reference comes from http://http.taiyangruanjian.com/news/59282.html

Guess you like

Origin blog.csdn.net/zhimaHTTP/article/details/112347021