SoFunction
Updated on 2024-12-18

An introduction to finding the number of daffodils using Python

I. Front-loading content

daffodil number(Narcissistic number is also known as a pluperfect digital invariant (PPDI), narcissistic number, self-power number, Armstrong number, or Armstrong number, and a daffodil number is a 3-digit number in which the sum of the powers of three of the digits in each digit equals itself. and is equal to itself. For example, 1^3 + 5^3+ 3^3 = 153.

1.1, traversal loop

for i in range(5):  
print(i)

1.2. Infinite loop

while(True):
print(1)

1.3. Cyclic control of reserved words

1.4 Advanced Usage of Loops

II. Case requirements

After learning the number of daffodils, I wanted to find the number of daffodils within 1000.
Input : None
Output : 153 370 371 407

III. Case analysis and resolution process

1、How to get the hundreds, tens and digits of a three-digit number?
ANSWER.
Getting the hundredths directly //100 gives you the hundredths
Get the tens digit directly %100 to get the tens digit + digit, and then to the remaining tens digit + digit / / 10 to get the tens digit
Get single digit direct pair %10

2. How to limit the range to 100 to 1000
Answer:Use for i in range(100 , 1000) , meaning iterates from 100 to 999.

IV. Complete code

# Find the number of daffodils within 1000
for i in range(100 , 1000) :  
# Get the hundreds
bai = i // 100  
# Get the tens digit
shi = i % 100 // 10  
# Get the digits
ge = i % 10  
# Find the third power of each number
result = pow(bai , 3 ) + pow(shi ,3 ) + pow(ge , 3);  
# To tell if it's a daffodil number #
if(i == result):  
print(i)

V. Inspection and acceptance codes

VI. Review of what has been learned

1、for i in range(N) loop N times

To this point this article on the use of Python to find the number of daffodils on the method of introduction to this article, more related Python to find the number of daffodils content please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!