Array ( [0] => [1] => questions [2] => Basic [3] => Armstrong-Number )
Given a integer N. You have to find the given number is Armstrong number or not. (Armstrong number is a number which is the sum of power of length of a given number to every digit is equal to given number).
First line contains a integer N.
Print Armstrong if it is Armstrong number else print not Armstrong.
153
Armstrong
145
Not Armstrong
53
Not Armstrong
Login to see Discussion
Approach 1:
Step-i) convert given number to string
Step-ii) calculate length of the string to get no of digits
Step-iii) initialize a variable to 0
Step-iv) get the remainder by dividing with 10
Step-v) raise the remainder to the power of length and add it to the variable
Step-vi) divide the number with 10
Step-vii) repeat steps iii, iv, v till the number is greater than 0
Step-viii) check whether the sum and the given number are same or not
Step-ix) if same return "Armstrong number" else return "Not Armstrong"
Time Complexity: O(n)+O(log(n)*log(log(n)))
O(n) for getting length of string, log(n) for traversing , for power function it is log(log(n)). It can be rounded to O(n)
Space Complexity: O(1)
Approach 2:
Step-i) use log function to get the length of the number
Step-ii) initialize a variable to 0
Step-iii) get the remainder by dividing with 10
Step-iv) raise the remainder to the power of length and add it to the variable
Step-v) divide the number with 10
Step-vi) repeat steps iii, iv, v till the number is greater than 0
Step-vii) check whether the sum and the given number are same or not
Step-viii) if same return "Armstrong number" else return "Not Armstrong"
Time Complexity: O(log(n)*log(log(n)))
log(n) for traversing , for power function it is log(log(n))
Space Complexity: O(1)
Note :
Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.
Login to see Solution