In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use Python programming language to check Armstrong numbers". In daily operation, I believe many people have doubts about how to use Python programming language to check Armstrong numbers. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Python programming language to check Armstrong numbers". Next, please follow the editor to study!
What is the Armstrong number?
If a positive integer is equal to the cubic sum of its numbers, the number is called the Armstrong number (also known as the narcissistic number).
A positive integer is called the Armstrong order.
Example:
Abcd... = an + bn + cn + dn +...
If it is a 3-digit Armstrong number, the cubic sum of each number is equal to the number itself.
For example:
# 153 = 1 "1" 1 + 5 "5" 5 + 3 "3" 3 / 153 is an Armstrong number.
II. Cases
1. Check Armstrong number (3 digits)
Example:
# Python program to check whether the number is an Armstrong number # accept the user's input num = int ("enter a number:") # initialize sum sum = 0 # find the cube sum of each number and temp = num while temp > 0: digit = temp% 10 sum + = digit * * 3 temp / / = 10 # display result if num = = sum: print (num Else: print (num, "not Armstrong number")
Output 1
Output 2
Code parsing:
Ask the user to enter a number, and then check whether it is an Armstrong number, you need to calculate the cube sum of each number.
Therefore, initialize the sum to 0 and use the modular operator (%) to get each number. The remainder of dividing a number by 10 is the last digit of the number. Use the exponential operator to get the cube.
Finally, the sum is compared with the original number and it is concluded that if equal, it is the Armstrong number.
two。 Check for Armstrong's n-digit number.
Example:
Num = 1634 # change the num variable to string # and calculate the length (digits) order = len (str (num)) # initialize sum sum = 0 # find the cube sum of each number temp = num while temp > 0: digit = temp% 10 sum + = digit * * order temp / / = 10 # display result if num = = sum: print (num, "is Armstrong number") else: print (num "not Armstrong numbers")
Running result:
Note:
Readers can change the value of num in the source code and then run it again to test it.
3. Find Armstrong numbers in integers
Example:
# Python program looks up Armstrong number lower = 100upper = 2000 for num in range (lower, upper + 1): # order number order = len (str (num)) # initialization sum sum = 0 temp = num while temp > 0: digit = temp% 10 sum + = digit * * order temp / / = 10 if num = sum: print (num)
Running result:
Note:
The lower limit of 100 is set in the variable lower and the upper limit of 2000 is set in the variable upper.
For loops are used to iterate from the variables lower to upper. During the iteration, the value of lower increases by 1 and checks to see if it is an Armstrong number.
You can change the scope and test by changing the variables lower and upper. The variable lower should be less than upper for this program to work properly.
At this point, the study on "how to use the Python programming language to check Armstrong numbers" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 227
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.