Print all Strong numbers less than or equal to N

15 Mar, 2021
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++
Java
Python3
C#
Javascript
// C++ program for the above approach#include<bits/stdc++.h>using namespace std; // Store the factorial of all the// digits from [0, 9]int factorial[] = { 1, 1, 2, 6, 24, 120,                    720, 5040, 40320, 362880 }; // Function to return true// if number is strong or notbool isStrong(int N){         // Converting N to String so that    // can easily access all it's digit    string num = to_string(N);      // sum will store summation of    // factorial of all digits    // of a number N    int sum = 0;      for(int i = 0; i < num.length(); i++)    {        sum += factorial[num[i] - '0'];    }      // Returns true of N is strong number    return sum == N;}  // Function to print all// strong number till Nvoid printStrongNumbers(int N){         // Iterating from 1 to N    for(int i = 1; i <= N; i++)    {                 // Checking if a number is        // strong then print it        if (isStrong(i))        {            cout << i << " ";        }    }} // Driver Codeint main(){         // Given number    int N = 1000;         // Function call    printStrongNumbers(N);         return 0;} // This code is contributed by rutvik_56
// Java program for the above approach class GFG {     // Store the factorial of all the    // digits from [0, 9]    static int[] factorial = { 1, 1, 2, 6, 24, 120,                               720, 5040, 40320, 362880 };     // Function to return true    // if number is strong or not    public static boolean isStrong(int N)    {         // Converting N to String so that        // can easily access all it's digit        String num = Integer.toString(N);         // sum will store summation of        // factorial of all digits        // of a number N        int sum = 0;         for (int i = 0;             i < num.length(); i++) {            sum += factorial[Integer                                 .parseInt(num                                               .charAt(i)                                           + "")];        }         // Returns true of N is strong number        return sum == N;    }     // Function to print all    // strong number till N    public static void    printStrongNumbers(int N)    {         // Iterating from 1 to N        for (int i = 1; i <= N; i++) {             // Checking if a number is            // strong then print it            if (isStrong(i)) {                System.out.print(i + " ");            }        }    }     // Driver Code    public static void        main(String[] args)            throws java.lang.Exception    {        // Given Number        int N = 1000;         // Function Call        printStrongNumbers(N);    }}
# Python3 program for the# above approach # Store the factorial of# all the digits from [0, 9]factorial = [1, 1, 2, 6, 24, 120,             720, 5040, 40320, 362880] # Function to return true# if number is strong or notdef isStrong(N):     # Converting N to String    # so that can easily access    # all it's digit    num = str(N)      # sum will store summation    # of factorial of all    # digits of a number N    sum = 0      for i in range (len(num)):        sum += factorial[ord(num[i]) -                         ord('0')]       # Returns true of N    # is strong number    if sum == N:       return True    else:       return False  # Function to print all# strong number till Ndef printStrongNumbers(N):         # Iterating from 1 to N    for i in range (1, N + 1):            # Checking if a number is        # strong then print it        if (isStrong(i)):            print (i, end = " ") # Driver Codeif __name__ == "__main__":       # Given number    N = 1000      # Function call    printStrongNumbers(N)     # This code is contributed by Chitranayal
// C# program for the above approachusing System;class GFG{ // Store the factorial of all the// digits from [0, 9]static int[] factorial = { 1, 1, 2, 6, 24, 120,                         720, 5040, 40320, 362880 }; // Function to return true// if number is strong or notpublic static bool isStrong(int N){     // Converting N to String so that    // can easily access all it's digit    String num = N.ToString();     // sum will store summation of    // factorial of all digits    // of a number N    int sum = 0;     for (int i = 0; i < num.Length; i++)    {        sum += factorial[int.Parse(num[i] + "")];    }     // Returns true of N is strong number    return sum == N;} // Function to print all// strong number till Npublic static void printStrongNumbers(int N){     // Iterating from 1 to N    for (int i = 1; i <= N; i++)    {         // Checking if a number is        // strong then print it        if (isStrong(i))        {            Console.Write(i + " ");        }    }} // Driver Codepublic static void Main(String[] args){    // Given Number    int N = 1000;     // Function Call    printStrongNumbers(N);}} // This code is contributed by sapnasingh4991
<script> // Javascript program for the above approach // Store the factorial of all the// digits from [0, 9]let factorial = [ 1, 1, 2, 6, 24, 120,                    720, 5040, 40320, 362880 ]; // Function to return true// if number is strong or notfunction isStrong(N){         // Converting N to String so that    // can easily access all it's digit    let num = N.toString();     // sum will store summation of    // factorial of all digits    // of a number N    let sum = 0;     for(let i = 0; i < num.length; i++)    {        sum += factorial[num[i] - '0'];    }     // Returns true of N is strong number    return sum == N;} // Function to print all// strong number till Nfunction printStrongNumbers(N){         // Iterating from 1 to N    for(let i = 1; i <= N; i++)    {                 // Checking if a number is        // strong then print it        if (isStrong(i))        {            document.write(i + " ");        }    }} // Driver Code         // Given number    let N = 1000;         // Function call    printStrongNumbers(N); // This code is contributed by Mayank Tyagi     </script>
1 2 145
 
Time Complexity: O(N)
sapnasingh4991
rutvik_56
ukasp
mayanktyagi1709
factorial
Analysis
Combinatorial
Greedy
Mathematical
Greedy
Mathematical
Combinatorial
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Understanding Time Complexity with Simple Examples
Time Complexity and Space Complexity
Analysis of Algorithms | Set 4 (Analysis of Loops)
Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete
Time Complexity of building a heap
Write a program to print all permutations of a given string
Permutation and Combination in Python
Factorial of a large number
Count of subsets with sum equal to X
itertools.combinations() module in Python to print all possible combinations