• Articles

What is Armstrong Number in Python?

Understanding Armstrong numbers can provide businesses with a unique perspective on number theory, potentially leading to innovative applications in data analysis and encryption. This blog will cover a range of topics, including what the Armstrong number actually is, various approaches to checking it, and practical implementation in Python programming language

Table of Contents

Check out this YouTube video specially designed for Python beginners.

Video Thumbnail

What is an Armstrong Number in Python?

An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits, each raised to the power of the number of digits. Here’s an example:

Consider the number 153:

1. It has three digits (1, 5, and 3).

2. We raise each digit to the power of three (the number of digits): 1^3, 5^3, and 3^3.

3. We add these figures together: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.

Because the sum of the digits raised to the power of the number of digits (3) equals the original number (153), 153 is an Armstrong number.

Consider another number 8208:

1. It has four digits (8, 2, 0, and 8).

2. We raise each digit to the power of four (the number of digits): 8^4, 2^4, 0^4, 8^4

3. We add these figures together: 8^4 + 2^4 + 0^4 + 8^4 =  4096 + 16 + 0 + 4096 = 8208.

Hence, 8208 is an Armstrong number.

Learn more about Python from this Python Data Science Course to get ahead in your career!

Various Methods to Check an Armstrong Number in Python

Let’s start with some theoretical information on the three techniques for testing Armstrong numbers in Python.

Approach 1: Using a For Loop 

  • This approach iterates through the digits of the number using a for loop. It initially converts the number to a string in order to count the digits.
  • It then repeats through the digits, raising each one to the power of the entire number of digits and adding the results to a running total.
  • It checks if the sum is equal to the original number after processing all of the digits.
  • This strategy is simple and easy to grasp, making it ideal for teaching purposes.

Approach 2: Using List Comprehension and the sum() function

  • This approach makes a list of the digits raised to the power of the total number of digits using list comprehension.
  • The sum() function is then used to compute the sum of the list’s elements.
  • Just like approach 1, this method also verifies whether the sum is equal to the original number.
  • This technique is short and efficient, taking advantage of list comprehension and sum capabilities of Python.

Approach 3: Making Use of a While Loop

  • To handle the digits of the number, a while loop is used. It keeps the original number for comparison purposes.
  • It loops through the integer, extracting the last digit with the modulo operator and multiplying it by the total number of digits.
  • The results are added to a running total, and the final digit is deleted by dividing by two.
  • It checks if the sum is equal to the original number after processing all of the digits.
  • This method is memory efficient because it does not generate a list of values.

Get 100% Hike!

Master Most in Demand Skills Now !

Algorithm to Find a 3-Digit Armstrong Number in Python

In Python, we start by extracting each digit in the integer, multiplying it three times (to calculate its cube), and then adding all those cubes of digits.

We will now compare the sum to the specified number. If the sum of the cubes equals the actual number, it is an Armstrong number; otherwise, it is not an Armstrong number.

Step 1: Check the number

Step 2: Create a variable number and set its value to 0.

Step 3: Multiply each number in the number 3 times and add the numbers.

Step 4: Determine if the numbers are equal to the starting number.

Step 5: If the two are equal, print it as an Armstrong number. Otherwise, copy this as a non-Armstrong number.

Preparing for Job Interview? Refer to Python Interview Questions to excel in your Interviews!

Program to Find Armstrong Number of 3 Digits

Using For Loop:

or num in range(100, 1000):
    # Calculate the sum of the cube of each digit
    digit_sum = 0
    original_num = num
    while num > 0:
        digit = num % 10
        digit_sum += digit ** 3
        num //= 10
    # Check if it's an Armstrong number
    if original_num == digit_sum and original_num >= 100:
        print(original_num)

Using While Loop:

def is_armstrong_number(num):
    num_str = str(num)
    num_digits = len(num_str)
    digit_sum = 0
    original_num = num
    while num > 0:
        digit = num % 10
        digit_sum += digit ** num_digits
        num //= 10
    return original_num == digit_sum
# Find Armstrong numbers of three digits
print("Armstrong numbers of three digits:")
for num in range(100, 1000):  # Three-digit numbers range from 100 to 999
    if is_armstrong_number(num):
        print(num)

Get ready for the high-paying Data Scientist jobs with these Top Data Science Interview Questions and Answers!

Algorithm to Find n-Digit Armstrong Number in Python

To determine whether an n-digit number is an Armstrong number, we first need to calculate the number equal to n.

We then multiply each number by the number, divide by itself n times (increase to the power of n), and add all these results to the corresponding different number.

Now we will see that the numbers are equal to the specified number. If both are equal, we report it as the Armstrong number or do not publish it at all.

 Step 1: Gather information.

Step 2: Determine the number and write it as a number.

Step 3: Create a variable number and set its value to 0.

Step 4: Multiply the number by the number and add to the number of each number

Step 5: Determine if the number is equal to the entry number.

Step 6: If equal, print as Armstrong number. Otherwise, do not write the Armstrong number.

Program to Find Armstrong Number of n Digits

Using For loop:

def is_armstrong_number(number):
    num_str=str(number)
    num_digits=len(num_str)
    total=0
    for digit_char in num_str:
        digit=int(digit_char)
        total+=digit ** num_digits
    return total==number
num=int(input("enter a number:"))   
if is_armstrong_number(num):
    print(num,"is an armstrong number")
else:
    print(num, "is not an armstrong number")

Using While loop:

def is_armstrong_number(number):
    num_str = str(number)
    num_digits = len(num_str)
    total = 0
    num = number  # Make a copy of the number for processing
    while num > 0:
        digit = num % 10
        total += digit ** num_digits
        num //= 10
    return total == number
num = int(input("Enter a number: "))
if is_armstrong_number(num):
    print(num, "is an Armstrong number.")
else:
    print(num, "is not an Armstrong number.")

End-Note

Armstrong numbers in Python are a fascinating mathematical concept. They hold the potential to enhance your business solutions. They invite exploration of number theory intricacies, paving the way for creative data analysis and encryption applications. Proficiency in Armstrong numbers can enrich your mathematical toolbox, offering innovative opportunities across diverse fields and exemplifying how math and programming can beautifully intersect in business contexts.

Course Schedule

Name Date Details
Python Course 05 Oct 2024(Sat-Sun) Weekend Batch
View Details
Python Course 12 Oct 2024(Sat-Sun) Weekend Batch
View Details
Python Course 19 Oct 2024(Sat-Sun) Weekend Batch
View Details