Have you ever heard of strong numbers?
If not yet, you are surely lagging behind in your computer fundamentals.
Well, the concept of strong numbers is always a given due weightage as questions related to finding the strong numbers are often asked from the aspirants.
A strong number is defined as the number when the sum of its factorial of given individual digits will be equal to that number.
Though, the concept is not limited to this!
Dig deep inside this concept and know the knits and grits of checking the strong number and the program to find the strong numbers.
Without any further ado, let’s get started!
Strong Number - An Overview
Before we get inside the approach or code of the strong numbers, let’s first start with its basic definition.
Strong number is defined as the number whose sum of the factorial of its digits will be equal to that particular number. For example; 1, 2 and 145 are some of the examples of the strong numbers.
Let’s discuss it with the help of a sample example.
Input given = 145
Output given = 145 [ it is a strong number]
Let’s get into its explanation;
1! + 4! + 5! = 1 + 24+ 120 = 145
As you can see that the factorial of all the given digits is equal to that number itself, you can consider this number as the strong number.
Let’s consider it with the help of another example:
The input given to you is 140
The output is also 140 but it is not a strong number
Let’s get into its explanation:
1 ! + 4! + 0 ! gives you 1 + 24 + 1 = 26
As in this case, the sum of the factorial of all the given digits will not be equal to that number itself, so it will not be considered as the strong number.
Approach to be followed
Though, the solution in this problem can be trivial. We can first extract all the digits of the given number n by taking it as the temporary number.
Let’s consider, temp is used to store each of the digits in that number n . we can find its factorial by adding each digit to its variable sum. Then we will compare it with the given number n. If both the numbers are equal, this will give you a strong number. Otherwise, if it is not equal, this is not a strong number.
The algorithm of this would be:
- Firstly, in order to send the number, we can use the function i.e findSumof Factorials
- Then extract each digit in the n by taking the remainder as 10. This will help you divide the number until its n >0. This can show the result of your sum variable
- At last, you need to check whether the n = = is sum or not, if the answer is yes, print the number as the strong number otherwise that number will not be a strong number
Implementation in C++
From 0 to 9, we are computing the factorial to use it wherever needed:
#include <stdio.h>
int factorial [10];
void precomputeFactorial ()
{
Factorial[0] = -1;
int mul = 1;
For [ int i = 1 ; i <=9, i ++]
}
}
}
int findSumof Factorials [ int n]
{
Int sum = 0;
While [ n > 0]
{
// find each digit
Int temp = n % 10;
// add factorial of each of the digit to its variable sum
sum + = factorial[temp];
n = n/ 10
}
Return sum;
}
int main []
{
int n = 145
// to precompute factorial of each digit and store in the array
precompute factorial();
// find the sum of factorial of each digit
int sum = find the sum of factorials [ n];
/* if the sum is equal to the original number
Then the number is a strong number */
if [ sum == n]
Printf (“%d is a strong number!”, n);
/* if the number is not equal to the original number */
Else
printf( “%d is not a strong number!”, n);
Return 0;
}
Output in this case would be 145 will be the strong number
Time complexity in this case would be the O [Log N]
While the loop n>0, you can run it at the most n number of times. Hence, you will get the time complexity.
The space complexity in this case would be O [1]
As we are not using any kind of explicit space, so the space complexity would be O [1]
Method to check strong number in C
To check the strong number in C we can follow some of the most ideal methods which are:
Using While Loop
In this case, we use a loop to extract the given digits so to find the factorial of each number.
We will use the int Factorial [ int n] which is a function to find the factorial of a given number.
The C program in this case will read your integer value by using the ‘ number’ variable. The digits in this case are extracted with the factorial of each digit. We can calculate the sum of each digit to print the number as the strong number.
Time complexity in this case would be O[ n]
Here n is representing the number of digits in a number. The factorial part may get neglected as a single digit will be taken so when you are extracting the digits, the number of steps in this case would be less.
Though, the required space complexity in this case of the program would be O [1] so we will not be using any of the extra space.
Additional Learning - Bridge in Computer Network
Bridge in computer network is defined as the networking device which is useful in connecting LANs to larger ones. The large LAN networks are being connected in this case with the smaller groups to transfer information in the best possible way.
Wrapping Up
Keep this guide as your mentor to know about the strong numbers in depth. Learn the knits and grits of strong numbers and check whether the given number is strong or not.
You are never too late to learn something new or exciting!