How to find Armstrong Number in Python using Recursion

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:

  1. Counting the number of digits in the given number.
  2. Raising each digit to the power of the total count and adding to a sum.
  3. 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 until n 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 of digit_count. It uses the modulus operator (%) to isolate the last digit of the number, raises this digit to the power of digit_count, and then adds the result to the result of calling is_armstrong on the rest of the number (achieved by dividing n by 10 using integer division).
  • The Python function check_armstrong counts the number of digits in n and then checks if n is equal to the result of calling is_armstrong on n. It returns True if n is an Armstrong number and False otherwise.

Output:

How to find Armstrong Number in Python using Recursion

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: