There are various categories of numbers on the basis of some set of rules like, if a number is divisible by two then it is an even number otherwise it will be an odd number. There could be more possible categories like prime number, composite number, twin prime number, and there are many more. Among these all there are special types of numbers known as Armstrong Numbers.
For a number, if the sum of all digits to their raised power equal to the length of the number is equal to the number itself then that number will be an armstrong number.
Let's take an example of 407:
-> Length of the 407 is 3 and if we take sum of all of its digit to raised power 3 that is 4*4*4 + 0*0*0 + 7*7*7 = 64 + 0 + 343 = 407. As we get the result equal to 407, which is our initial number that means 407 is an armstrong number.
We will discuss two algorithms:
Steps for the algorithm:
Let’s take an example to implement the above given approach for better understanding:
#include <math.h>
#include <stdio.h>
int main(){
// initial number
int n = 407;
// creating extra variable to store initial number
int temp_n = n;
// store the number of digits of n in count
int count = 0;
while(temp_n)
{
temp_n = temp_n / 10; // keep dividing by 10
count++;
}
// as temp_n becomes zero again assigning it n
temp_n = n;
// finding the required sum
int sum = 0;
while(temp_n)
{
// a contains the last digit
int a = temp_n % 10;
// store the sum of the power of individual digits in variable sum
sum += pow(a, count);
temp_n = temp_n / 10; // keep dividing
}
// if sum is equal to n, the number is an Armstrong number
if (sum == n)
{
printf("%d is an Armstrong number.", n);
}
else
{
printf("%d is not an Armstrong number.", n);
}
return 0;
}
Output:
407 is an Armstrong number
In the above approach, we keep dividing the current number by 10 until it becomes zero, that is log10(N) times. After that for every digit we raised their power to the number of digits, that is log10(N).
So final complexity if O(log10(N))2.
In the above approach, we haven’t used extra space. So space complexity is O(1).
Steps for the algorithm:
Lets see an example on the above given approach for better understanding:
#include <math.h>
#include <stdio.h>
// function to check whether number is Armstrong or not
bool is_armstrong(int n)
{
// creating extra variable to store initial number
int temp_n = n;
// store the number of digits of n in count
int count = 0;
while(temp_n)
{
temp_n = temp_n / 10; // keep dividing by 10
count++;
}
// as temp_n becomes zero again assigning it n
temp_n = n;
// finding the required sum
int sum = 0;
while(temp_n)
{
// a contains the last digit
int a = temp_n % 10;
// store the sum of the power of individual digits in variable sum
sum += pow(a, count);
temp_n = temp_n / 10; // keep dividing
}
// if sum is equal to n, return true
if (sum == n)
{
return true;
}
else
{
return false;
}
}
int main(){
int start = 100, end = 1000;
printf("Print all Armstrong numbers between %d and %d:\n",start,end);
// for checking each number is Armstrong number or not, traversing a loop over them
// alos, we will maintain count of number of Armstrong number
int count = 0;
for(int i = start; i <= end;i++){
// calling function to check whether current number is Armstrong or not
if(is_armstrong(i))
{
// increase the count
count++;
printf("Armstrong number is:%d\n", i);
}
}
printf("Total %d number is an Armstrong number between 1 to 1000.", count );
return 0;
}
Output:
Print all Armstrong numbers between 100 and 1000:
Armstrong number is:370
Armstrong number is:371
Armstrong number is:407
Total 3 number is an Armstrong number between 1 to 1000.
In the above approach, we iterate over all of the numbers which take O(N) time where N is the number of elements in the range. Also, as we have seen in the above section, for number M, time complexity to check whether it is an armstrong number or not, takes O(log10(M))2.
So final complexity if N*O(log10(N))2.
In the above approach, we haven’t used extra space. So space complexity is O(1).