Print all Strong numbers less than or equal to N
Given a number N, print all the Strong Numbers less than or equal to N.
Strong number is a special number whose sum of the factorial of digits is equal to the original number.
For Example: 145 is strong number. Since, 1! + 4! + 5! = 145.
Examples:
Input: N = 100
Output: 1 2
Explanation:
Only 1 and 2 are the strong numbers from 1 to 100 because
1! = 1, and
2! = 2
Input: N = 1000
Output: 1 2 145
Explanation:
Only 1, 2 and 145 are the strong numbers from 1 to 1000 because
1! = 1,
2! = 2, and
(1! + 4! + 5!) = 145
Approach: The idea is to iterate from [1, N] and check if any number between the range is strong number or not. If yes then print the corresponding number, else check for the next number.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int factorial[] = { 1, 1, 2, 6, 24, 120,
720, 5040, 40320, 362880 };
bool isStrong( int N)
{
string num = to_string(N);
int sum = 0;
for ( int i = 0; i < num.length(); i++)
{
sum += factorial[num[i] - '0' ];
}
return sum == N;
}
void printStrongNumbers( int N)
{
for ( int i = 1; i <= N; i++)
{
if (isStrong(i))
{
cout << i << " " ;
}
}
}
int main()
{
int N = 1000;
printStrongNumbers(N);
return 0;
}
|
Java
class GFG {
static int [] factorial = { 1 , 1 , 2 , 6 , 24 , 120 ,
720 , 5040 , 40320 , 362880 };
public static boolean isStrong( int N)
{
String num = Integer.toString(N);
int sum = 0 ;
for ( int i = 0 ;
i < num.length(); i++) {
sum += factorial[Integer
.parseInt(num
.charAt(i)
+ "" )];
}
return sum == N;
}
public static void
printStrongNumbers( int N)
{
for ( int i = 1 ; i <= N; i++) {
if (isStrong(i)) {
System.out.print(i + " " );
}
}
}
public static void
main(String[] args)
throws java.lang.Exception
{
int N = 1000 ;
printStrongNumbers(N);
}
}
|
Python3
factorial = [ 1 , 1 , 2 , 6 , 24 , 120 ,
720 , 5040 , 40320 , 362880 ]
def isStrong(N):
num = str (N)
sum = 0
for i in range ( len (num)):
sum + = factorial[ ord (num[i]) -
ord ( '0' )]
if sum = = N:
return True
else :
return False
def printStrongNumbers(N):
for i in range ( 1 , N + 1 ):
if (isStrong(i)):
print (i, end = " " )
if __name__ = = "__main__" :
N = 1000
printStrongNumbers(N)
|
C#
using System;
class GFG{
static int [] factorial = { 1, 1, 2, 6, 24, 120,
720, 5040, 40320, 362880 };
public static bool isStrong( int N)
{
String num = N.ToString();
int sum = 0;
for ( int i = 0; i < num.Length; i++)
{
sum += factorial[ int .Parse(num[i] + "" )];
}
return sum == N;
}
public static void printStrongNumbers( int N)
{
for ( int i = 1; i <= N; i++)
{
if (isStrong(i))
{
Console.Write(i + " " );
}
}
}
public static void Main(String[] args)
{
int N = 1000;
printStrongNumbers(N);
}
}
|
Javascript
<script>
let factorial = [ 1, 1, 2, 6, 24, 120,
720, 5040, 40320, 362880 ];
function isStrong(N)
{
let num = N.toString();
let sum = 0;
for (let i = 0; i < num.length; i++)
{
sum += factorial[num[i] - '0'];
}
return sum == N;
}
function printStrongNumbers(N)
{
for (let i = 1; i <= N; i++)
{
if (isStrong(i))
{
document.write(i + " " );
}
}
}
let N = 1000;
printStrongNumbers(N);
</script>
|
Time Complexity: O(N log10N)
Auxiliary Space: O(log10N)
Last Updated :
16 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...