filmov
tv
Python One-Liner: Check if a Number is an Armstrong Number! 🔢✨ #PythonTips #codingshorts

Показать описание
🔢 Check if a Number is an Armstrong Number in One Line!
This script demonstrates two methods to determine whether a given number is an Armstrong number. An Armstrong number is one that is equal to the sum of its own digits each raised to the power of the number of digits in the number.
Long Way Explanation:
Convert to String & Extract Digits:
The function converts the number to a string so it can iterate over each digit. It then converts each digit back to an integer.
Determine the Power:
The number of digits (the length of the string) is used as the power.
Compute the Armstrong Sum:
It calculates the sum of each digit raised to the power determined in the previous step.
Comparison:
Finally, it compares the calculated sum with the original number. If they are equal, the number is an Armstrong number.
One-Liner Explanation:
Inline Conversion & Calculation:
The one-liner performs all the steps inline. It converts the number to a string, iterates over each character, converts each to an integer, raises it to the power of the length of the string (number of digits), and sums these values.
Direct Comparison & Print:
The result of the sum is directly compared to the original number, and the boolean result is printed.
This approach not only highlights Python’s ability to handle mathematical operations in a concise manner but is also an excellent demonstration for coding interviews and challenges! 🚀
#Python #OneLiner #ArmstrongNumber #PythonTips #CodingShorts #LearnPython #Programming
📌 Long Way:
def is_armstrong(n):
# Convert the number to a string to extract its digits
digits = [int(d) for d in str(n)]
# The power is the number of digits
power = len(digits)
# Calculate the sum of each digit raised to the power of the number of digits
armstrong_sum = sum(d ** power for d in digits)
# Check if the calculated sum equals the original number
return n == armstrong_sum
# Example usage:
number = 153
print(is_armstrong(number)) # Output: True
📌 One-Liner:
n = 153; print(n == sum(int(d)**len(str(n)) for d in str(n)))
# Output: True
This script demonstrates two methods to determine whether a given number is an Armstrong number. An Armstrong number is one that is equal to the sum of its own digits each raised to the power of the number of digits in the number.
Long Way Explanation:
Convert to String & Extract Digits:
The function converts the number to a string so it can iterate over each digit. It then converts each digit back to an integer.
Determine the Power:
The number of digits (the length of the string) is used as the power.
Compute the Armstrong Sum:
It calculates the sum of each digit raised to the power determined in the previous step.
Comparison:
Finally, it compares the calculated sum with the original number. If they are equal, the number is an Armstrong number.
One-Liner Explanation:
Inline Conversion & Calculation:
The one-liner performs all the steps inline. It converts the number to a string, iterates over each character, converts each to an integer, raises it to the power of the length of the string (number of digits), and sums these values.
Direct Comparison & Print:
The result of the sum is directly compared to the original number, and the boolean result is printed.
This approach not only highlights Python’s ability to handle mathematical operations in a concise manner but is also an excellent demonstration for coding interviews and challenges! 🚀
#Python #OneLiner #ArmstrongNumber #PythonTips #CodingShorts #LearnPython #Programming
📌 Long Way:
def is_armstrong(n):
# Convert the number to a string to extract its digits
digits = [int(d) for d in str(n)]
# The power is the number of digits
power = len(digits)
# Calculate the sum of each digit raised to the power of the number of digits
armstrong_sum = sum(d ** power for d in digits)
# Check if the calculated sum equals the original number
return n == armstrong_sum
# Example usage:
number = 153
print(is_armstrong(number)) # Output: True
📌 One-Liner:
n = 153; print(n == sum(int(d)**len(str(n)) for d in str(n)))
# Output: True