Python Program to Get Sum of N Armstrong Number
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.
Python3
# 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.
Python3
# 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)
Please Login to comment...