1 min to read
Check for Armstrong Number
What is an Armstrong number?
An Armstrong number is a number that is equal to the sum of the cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.
Armstrong number example
For example, 153 is an Armstrong number because 153 = 111 + 555 + 333. Another example of Armstrong number is 371 because 371 = 333 + 777 + 111.
Problem Statement:
Write a program to check the number is Armstrong number is not.
Check Armstrong number in python
Share your solution to check Armstrong number.
Check All Armstrong number in python
Check Armstrong number in C++
Explanation
- First, we will define a function
is_armstrong_number
which will take a number as an argument and return True if the number is Armstrong number else return False. - Second, we will loop over the number and calculate the sum of the cubes of its digits and if the sum is equal to the number then we will return True else False.
Complexity Analysis
- Time complexity : O(n). We traverse the list containing n elements only once.
- Space complexity : O(1). Constant extra space is used.
Conclusion
In this post, we have seen how to check for Armstrong number in python. If you want to solve more such problems please visit Project Euler
Thank you for reading, pls keep visiting this blog and share this in your network.