Python Program to Get Sum of N Armstrong Number
Last Updated :
05 Sep, 2024
Given a number N, determine the sum of the first N Armstrong numbers using Python.
Example:
Input : 11
Output : 568
First 11 Armstrong numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, lies to, 370
Their summation is 578
Method 1: Using Iterative methods
- Create a while loop that breaks when the desired number of Armstrong numbers is found.
- At each iteration, the current value will be incremented by 1.
- The idea is to first count the number of digits present in the current number.
- Let us assume the number of digits be d. For every digit x in the current number, compute x**d. If the sum of all such values is equal to the current number then add that element to the result.
- Finally, return the result.
Python
# Function to calculate order of the number
def order(x):
# returns the length of the number
return len(str(x))
# Function to check whether the given number is
# Armstrong number or not
def isArmstrong(x):
k = order(x) # order of the number
cur = x
sum1 = 0
while (cur != 0):
# For every digit r in the current number, compute r**k.
r = cur % 10
sum1 = sum1 + int(r**k)
cur = cur//10
# If sum of all such values is equal to the current number
# then add that element to result.
return (sum1 == x)
# Python program to determine the sum of
# first n armstrong numbers
def getSumOfArmstrong(n):
cur = 1
result = 0
cur_elems = 0
# numbers found so far.
while cur_elems != n:
if isArmstrong(cur):
result += cur
cur_elems += 1
cur += 1
return result
print(getSumOfArmstrong(11))
Output:
568
Complexity Analysis:
Let n be the Nth Armstrong number..
Time Complexity: O(n * len(n))
Space Complexity: O(1)
Method 2: Using recursion method
Instead of using the iterative approach, we can use a recursive approach to check whether the number is Armstrong number or not and also to find order of a number.
We will pass the length of the function and the number itself as parameters to the recursive function.
In each recursive call it will find the digit present in the units place raised to the order of number and calls the function again for the remaining digits excluding the units digit.
Python
# Function to calculate order of the number
def order(x):
# Increments the length by 1 for
# current units digit and finds
# orders for remaining digits.
if x//10 == 0:
return 1
# returns the length of the number
return 1+order(x//10)
# Function to check whether the given number is
# Armstrong number or not
def isArmstrong(x, order):
if x == 0:
return 0
if x != 0:
# digit present in the units place raised
# to the order of number and calls the
# function again for remaining digits
# excluding the units digit.
return int(pow(x % 10, order))+isArmstrong(x//10, order)
# Python program to determine the sum of
# first n armstrong numbers
def getSumOfArmstrong(n):
cur = 1
result = 0 # Variable to store final result
cur_elems = 0 # Variable to count total number of Armstrong
# numbers found so far.
while cur_elems != n: # Iterating until we get n armstrong numbers
# Checking current number armstrong is or not.
if isArmstrong(cur, order(cur)) == cur:
result += cur # Storing sum of Armstrong numbers.
cur_elems += 1
cur += 1
return result
print(getSumOfArmstrong(11))
Output:
568
Complexity Analysis:
Let n be the Nth Armstrong number..
Time Complexity: O(n * len(n))
Space Complexity: O(n) (For recursive call stack)
Similar Reads
Python program to calculate square of a given number The task of calculating the square of a number in Python involves determining the result of multiplying a number by itself. For example, given the number 4, its square is 16 because 4 Ã 4 = 16.Using ** operatorexponentiation operator (**) is the most direct and optimized way to compute powers. Since
1 min read
Python Program to Find Armstrong Number in an Interval We are given an interval, that is a start and an end value. We have to find all the Armstrong numbers in the given range. In this article, we will discuss how to find the Armstrong Number in an interval in Python.Armstrong NumberA number is referred to as an Armstrong number if the sum of each digit
4 min read
Python Program for cube sum of first n natural numbers We are given a number n and we need to print the sum of series 13 + 23 + 33 + 43 + .......+ n3 till the n-th term in python.Examples:Input: n = 5Output: 225Explanation: 13 + 23 + 33 + 43 + 53 = 225Let's discuss some of the ways to do it.Using Mathematical Formula: Most efficient solution is to use t
2 min read
Python Program to Find Sum of First and Last Digit Given a positive integer N(at least contain two digits). The task is to write a Python program to add the first and last digits of the given number N. Examples: Input: N = 1247 Output: 8 Explanation: First digit is 1 and Last digit is 7. So, addition of these two (1 + 7) is equal to 8.Input: N = 73
5 min read
Python program to find sum of elements in list Finding the sum of elements in a list means adding all the values together to get a single total. For example, given a list like [10, 20, 30, 40, 50], you might want to calculate the total sum, which is 150. Let's explore these different methods to do this efficiently.Using sum()sum() function is th
3 min read