Hello curious minds! In this article we’ll explore the Python program to check Armstrong Number. Explained comprehensively with examples, code, edge cases, dry run, time and space complexity.
Here is the [Problem Link] for your quick reference.
Examples of Python Program to Check Armstrong Number:
Example 1:
Input: N = 153
Output: True
Explanation: 13 + 53 + 33 = 1 + 125 + 27 = 153
Example 2:
Input: N = 371
Output: True
Explanation: 33 + 53 + 13 = 27 + 343 + 1 = 371
Solution
Problem Statement:
Objective: We aim to determine whether a given integer “num” is an Armstrong number (also known as a narcissistic 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.
Purpose: The function “isArmstrong” checks if the integer “num” is an Armstrong number by calculating the sum of its digits each raised to the power of the number of digits and comparing it to the original number.
Expected Input and Output:
- Input: A positive integer “num”.
- Output: A boolean value True if “num” is an Armstrong number, and False otherwise.
Also read Palindrome Number Program in Python | Leetcode #9
Intuition and Approach:
Intuition: To determine if a number is an Armstrong number, we can break the number into its individual digits, raise each digit to the power of the total number of digits, and sum these values. If the sum equals the original number, then it is an Armstrong number.
Approach:
- Convert the number to a string to determine the number of digits
- Initialize a variable to store the sum of the digits each raised to the power of the number of digits
- Use a loop to extract each digit, raise it to the appropriate power, and add the result to the sum
- Compare the computed sum to the original number to determine if it is an Armstrong number
Code:
def isArmstrong(num):
k = len(str(num))
arm_sum = 0
n = num
while n > 0:
ld = n % 10
arm_sum += ld**k
n = n // 10
return arm_sum == num
- Number of Digits: The variable “k” is set to the length of the string representation of “num”, giving the number of digits in “num”.
- Initialize Sum: The variable “arm_sum” is initialized to 0 to accumulate the sum of the digits raised to the power of “k”.
- Loop to Calculate Sum:
- While “n” is greater than 0, the last digit “ld” is extracted using n % 10
- This digit is raised to the power of “k” and added to “arm_sum”
- The last digit is removed from “n” using integer division by 10
- Return Comparison: The function returns True if “arm_sum” is equal to “num”, and False otherwise.
Dry Run:
Let’s walk through a step-by-step execution with a sample image given below, this example demonstrates when a number is not an Armstrong number.
Potential Edge Cases:
- Single-Digit Numbers:
- Any single-digit number should return True as any number is equal to itself raised to the power of one (e.g., num = 5).
- Zero:
- num = 0 should return True as 0^1 = 0
- Large Numbers:
- The function should handle large numbers correctly and check if they are Armstrong numbers.
Handling Edge Cases:
- The current code handles single-digit numbers and zero correctly.
- It also works for large numbers due to Python’s ability to handle large integers.
Time and Space Complexity:
Time Complexity: “O(log10N + 1)” where “N” is the input number. The time complexity is determined by the number of digits in the input integer “N”. In the worst case when “N” is a multiple of 10 the number of digits in N is “log10 N + 1”.
- In the while loop we divide N by 10 until it becomes 0 which takes “log10N” iterations.
- In each iteration of the while loop we perform constant time operations like modulus and division and pushing elements into the vector.
Space Complexity: “O(1)” as only a constant amount of additional memory for the reversed number regardless of size of the input number.
For any changes to the document, kindly email at code@codeanddebug.in or contact us at +91-9712928220.