How to check for Armstrong Number in Python?

How to check for Armstrong Number in Python?

armstrong-number-in-python

How to check for Armstrong Number in Python?

How to check for Armstrong Number in Python?

How to check for Armstrong Number in Python?

WhatsApp
LinkedIn
Pinterest

How to check for Armstrong Number in Python?

How to check for Armstrong Number in Python?

Have you ever heard about Armstrong Numbers? They’re also called narcissistic or pluperfect digital invariant numbers. 

An Armstrong number is like a star among numbers. It equals the sum of its digits, each raised to the digit count’s power. 

Take 153. 

It has 3 digits. 

Calculate:- 1^3 + 5^3 + 3^3. If it’s 153, it’s magic!

Today, we will see how to find these numbers in Python. Basic way with loops and logic. We are going to cover all the approaches to find these numbers.

Whether new or expert, this article’s for you. Unraveling Armstrong numbers with Python, step by step. 

What is Armstrong number?

A number which is equal to the sum of all of its digits powered by the number of the digits is an Armstrong Number.

Like 153:- 1^3 + 5^3 + 3^3 = 153.

Henry S. Armstrong discovered them in 1905. Also called narcissistic numbers.

armstrong-number-in-python (1)

How to find them? Here’s one way:

  • Split number into digits:- 153 – 1 5 3
  • Cube digits:- 1^3=1 5^3=125 3^3=27
  • Add:- 1 + 125 + 27 = 153
  • If sum = number, it’s Armstrong.

Interesting Facts:

  • No 2-digit Armstrongs.
  • Count of the Armstrong Numbers drops with more digits.
  • Biggest known: 39 digits.
  • All 1-digit are Armstrong.
  • Sum of first n Armstrongs is Armstrong.

 

Read about more interesting programs like this:-

Basic Python Programs

Linear search in Python

Identity Operator in Python

 

Program to find 3 digit Armstrong number in python 

 

Output:

 

How this code works:

  • Function Definition:
    • is_armstrong function checks if a number is an Armstrong number.
    • Converts the number to a string to determine the digit count.
    • Calculates the sum of each digit raised to the power of digit count.
    •  if sum is equal to the number:- Returns True
  • List Comprehension:
    • Generates a list of numbers from 100 to 999.
    • Checks each number using is_armstrong function.
    • Collects Armstrong numbers in the list.
  • Printing Results:
    • Prints the list of Armstrong numbers with 3 digits.

 

Program to find n digit Armstrong number in python 

Input:

 

Output:

 

How this code works:

  • Function is_armstrong:
    • Checks if a number is an Armstrong number.
    • Same logic as previously described.
    • Function n_digit_armstrong_numbers:
  • Takes n as input.
    • Calculates lower and upper limits for n-digit numbers.
    • Generates a list of numbers within that range.
    • Checks each number using is_armstrong function.
  • Main Part of Code:
    • Takes user input for n.
    • Calls n_digit_armstrong_numbers to find n-digit Armstrong numbers.
    • Prints the resulting list of Armstrong numbers.

 

1. Using Loops and String Conversion

The approach is simple; it only consists of 4 steps.

  1. Convert the number to a string.
  2. Iterate over the digits using a loop.
  3. Raise the digit by the power of the number of digits
  4. Calculate the sum.

 

Input:

 

Output:

 

How this code works:

is_armstrong Function:

  • Converts input number to string to count digits.
  • Loop iterates over each digit, converting it to integer.
  • Digit raised to power of total digits, added to total.
  • Returns whether total equals original number.

Main Code:

  • Range of numbers from start to end is iterated.
  • is_armstrong function is applied to each number.

 

2. Using List Comprehension and String Conversion

The approach is also very simple; it only consists of 3 steps.

  • Convert the number to a string.
  • Utilize list comprehension for concise iteration.
  • Raise each digit to the power of total digits and sum them up.

 

Input:

 

OutPut:

 

How this code works:

is_armstrong Function:

  • Accepts a number, counts its digits using string conversion.
  • Utilizes list comprehension for concise calculation.
  • Computes the sum of each digit raised to total digits’ power.
  • Check if the calculated sum is equal to the number.

Main Code:

  • The range of numbers from start to end is iterated using a list comprehension.
  • The is_armstrong function filters Armstrong numbers using the comprehension.

Using Recursion

Recursion for Armstrong Numbers is an alternative method using recursion. Involves breaking down the number into digits and summing them recursively.

It’s programming style is more functional and elegant.

 

Input:

 

OutPut:

 

How this code works:

is_armstrong Function:

  • Accepts a number, counts its digits using string conversion.
  • Utilizes list comprehension for concise calculation.
  • Computes the sum of each digit raised to total digits’ power.
  • Check if the calculated sum is equal to the number.

Main Code:

  • The range of numbers from start to end is iterated using a list comprehension.
  • The is_armstrong function filters Armstrong numbers using the comprehension.

 

Using itertools.product for Brute Force

This is a different approach that will give a very unique point of view. Generates all possible digit combinations.

Checks if any combination forms an Armstrong number. Computationally intensive but showcases innovative thinking.

 

Input:

 

Output:

 

Using itertools.product for Armstrong Numbers:

  • Utilizes itertools.product function for unique strategy.
  • Generates all digit combinations within the range.
  • Computes the sum of digits’ powers in each combination.
  • Identifies Armstrong numbers among the combinations.

 

Here are some interesting programs in Java:-

Magic Number in Java

Fibonacci series in Java

Reverse a number in Java

Swap two numbers

Addition of two numbers

Java Tokens

 

Conclusion

  • We have discussed methods of finding Armstrong number in python here.
  • Standard loops, functional recursion, and brute-force are covered.
  • You may code your own based on your style and needs.


Aniket

Aniket

Leave a Reply

Your email address will not be published. Required fields are marked *

Blogs You May Like

Get in touch to claim Best Available Discounts.

If You Are Looking for Job Assistance Please Fill Up the Form.

× How can I help you?