There is a category of numbers that are called"Number of daffodils", also known as the "narcissistic number" and "Armstrong number". Daffodil number is a special class of natural numbers, and daffodil number refers to a 3-digit numberThe sum of the cubes of the numbers on each of its bits is equal to itself。This article will use Python as a tool to show the ingenuity of programming by writing a program with daffodils. **Can be shared.
For example: abc = a3 + b3 + c3
For example, 153 is a daffodil number because (1 3 + 5 3 + 3 3 = 153).
Let's write a simple python program step by step for finding daffodil numbers.
First, we need to get the range of user input to determine in which interval to look for the number of daffodils. We can use itinput
function to implement this step. Note that the number of daffodils is a three-digit number, so the input range should be reasonable. For example, 123 at the beginning and 456 at the end.
start = int(input("Please enter a starting range:"))end = int(input("Please enter the end range:"))
Next, we use the loop to iterate through each number in the specified range and check if it's the daffodil number.
for num in range(start, end + 1):Here's howGet every digit.
temp = num
sum = 0
while temp > 0:
digit = temp % 10
sum1 += digit **3
temp //= 10
Determine whether it is the number of daffodils.
if num == sum1:
print(num)
for
How loops work:
Each number in the iteration range:Passedfor num in range(start, end + 1)
, we traversed all the numbers from the start range to the end range, including the boundary values.
Get the sum of the cubes of each digit:For each numbernum
, we use a temporary variabletemp
to savenum
A copy of the . Then, pass onewhile
Cycle, we start withtemp
, take the numbers bit by bit, calculate their cubes, and add up the results tosum1
variables. While loop by taking the remainder operationtemp % 10
Gettemp
the single digit of the calculated sum of its cubes and willtemp
Updated to remove the remaining single digits. This process goes on and on untiltemp
becomes 0.
Determine whether it is the number of daffodils. In each iteration, we checknum
Whether it is equal tosum
If it is equal, it means that the number is the number of daffodils.
Finally, we put together the above ** and run the program to see which daffodil counts are found within the specified range.
start = int(input("Please enter a starting range:"))By running this program, you will get a list of the number of daffodils within the specified range. For example, 123 at the beginning and 456 at the end. The corresponding results are as follows:end = int(input("Please enter the end range:"))
for num in range(start, end + 1):
temp = num
sum1 = 0
while temp > 0:
digit = temp % 10
sum1 += digit **3
temp //= 10
if num == sum1:
print(num)
Of course, there are other ways to program the number of daffodils. Let's take a look at one way to use list comprehension and one way to use functions. List inference is a concise yet powerful way to build lists in Python that we can use to find out the number of daffodils. Here's the corresponding **:
start = int(input("Please enter a starting range:"))This method uses list derivation and string iteration to calculate the sum of the cubes on each bit of each number, and filter out the number of daffodils through conditional judgment. This is more concise, but may be slightly less performant than the loop approach. Again,Start 123, end 456.end = int(input("Please enter the end range:"))
Use list derivation to find the number of daffodils.
narcissistic_numbers = [num for num in range(start, end + 1) if num == sum(int(digit) *3 for digit in str(num))]
print(narcissistic_numbers)
We can also encapsulate the process of judging the number of daffodils into a function to improve the readability and reusability of **
def is_narcissistic(num):Here's how, 123 at the beginning and 456 at the end. Get the same result.return num == sum(int(digit) *3 for digit in str(num))
start = int(input("Please enter a starting range:"))
end = int(input("Please enter the end range:"))
Use the function to find out the number of daffodils.
narcissistic_numbers = [num for num in range(start, end + 1) if is_narcissistic(num)]
print(narcissistic_numbers)
This method is done through functionsis_narcissistic
Encapsulates the logic of judging the number of daffodils to make the main program clearer. Each of these three methods has its own characteristics, and no matter which method is used, it can achieve the same effect, demonstrating the flexibility and diversity of Python programming.
By writing this simple program for daffodil counting, we not only learned how to use Python for basic user input and looping, but also understood the math behind daffodil counting. Through this blog, I hope we have a deeper understanding of the number of daffodils and can write simple programs.