In this Python Tutorial, we will how to find the Armstrong number in Pythin using recursion. Moreover, we will learn why we use recursion, its explanation, and example.
What is Armstrong Number
An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its digits each raised to the power of the number of digits. For example, 371 is an Armstrong number because 3³ + 7³ + 1³ = 371.
Why Use Recursion?
Recursion is a method of solving problems that involves breaking a problem down into smaller and smaller sub-problems until you get to a small enough problem that it can be solved trivially. The advantage of recursion is that an algorithm can be defined in terms of itself.
In the case of finding the Armstrong number, we’ll use recursion to calculate the sum of the digits raised to the appropriate power.
Algorithm Explanation of Armstrong Number using Recursion
Our recursive algorithm works by:
- Counting the number of digits in the given number.
- Raising each digit to the power of the total count and adding to a sum.
- Continuing to do this recursively until we’ve processed each digit in the number.
Below is the Python code to find out whether a given number is an Armstrong number using recursion:
def count_digits(n):
if n == 0:
return 0
return 1 + count_digits(n // 10)
def is_armstrong(n, digit_count):
if n == 0:
return 0
return ((n % 10) ** digit_count) + is_armstrong(n // 10, digit_count)
def check_armstrong(n):
digit_count = count_digits(n)
return n == is_armstrong(n, digit_count)
number = int(input("Enter a number: "))
print(check_armstrong(number))
Here is a breakdown of what the code is doing:
- The Python function
count_digits
counts the number of digits in a number. It uses the integer division operator (//
) to divide the number by 10, effectively removing the last digit, and calls itself on the result. This is repeated untiln
is 0, at which point the function returns 0. - The Python function
is_armstrong
calculates the sum of each digit in the number raised to the power ofdigit_count
. It uses the modulus operator (%
) to isolate the last digit of the number, raises this digit to the power ofdigit_count
, and then adds the result to the result of callingis_armstrong
on the rest of the number (achieved by dividingn
by 10 using integer division). - The Python function
check_armstrong
counts the number of digits inn
and then checks ifn
is equal to the result of callingis_armstrong
onn
. It returnsTrue
ifn
is an Armstrong number andFalse
otherwise.
Output:
Conclusion
We have successfully implemented a recursive function in Python to determine whether a number is an Armstrong number. Remember that while recursion can make algorithms easier to understand and implement, it can also lead to increased memory usage and potential stack overflow if the recursive call stack gets too large.
You may also like to read the following articles:
- Multiply in Python with Examples
- Add zeros before a number in Python
- How to reverse a number in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.