Find all factorial numbers less than or equal to n - GeeksforGeeks

15 Mar, 2021
A number N is called a factorial number if it is the factorial of a positive integer. For example, the first few factorial numbers are1, 2, 6, 24, 120, ...Given a number n, print all factorial numbers smaller than or equal to n. Examples :  
Input : n = 100
Output : 1 2 6 24

Input : n = 1500
Output : 1 2 6 24 120 720
 
A simple solution is to generate all factorials one by one until the generated factorial is greater than n.An efficient solution is to find next factorial using previous factorial.  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find all factorial numbers// smaller than or equal to n.#include <iostream>using namespace std; void printFactorialNums(int n){    int fact = 1;    int x = 2;    while (fact <= n) {        cout << fact << " ";         // Compute next factorial        // using previous        fact = fact * x;         x++;    }} // Driver codeint main(){    int n = 100;    printFactorialNums(n);    return 0;}
// Java program to find all factorial numbers// smaller than or equal to n. class GFG{    static void printFactorialNums(int n)    {        int fact = 1;        int x = 2;        while (fact <= n)        {            System.out.print(fact + " ");                 // Compute next factorial            // using previous            fact = fact * x;                 x++;        }    }         // Driver code    public static void main (String[] args)    {        int n = 100;        printFactorialNums(n);    }} // This code is contributed by Anant Agarwal.
# Python3 program to find all factorial# numbers smaller than or equal to n. def printFactorialNums( n):    fact = 1    x = 2    while fact <= n:        print(fact, end = " ")                 # Compute next factorial        # using previous        fact = fact * x                 x += 1 # Driver coden = 100printFactorialNums(n) # This code is contributed by "Abhishek Sharma 44"
// C# program to find all factorial numbers// smaller than or equal to n.using System; class GFG{    static void printFactorialNums(int n)    {        int fact = 1;        int x = 2;        while (fact <= n)        {            Console.Write(fact + " ");                 // Compute next factorial            // using previous            fact = fact * x;                 x++;        }    }         // Driver code    public static void Main ()    {        int n = 100;        printFactorialNums(n);    }} // This code is contributed by vt_m.
<?php// PHP program to find all// factorial numbers smaller// than or equal to n. function printFactorialNums($n){    $fact = 1;    $x = 2;    while ($fact <= $n)    {        echo $fact, " ";         // Compute next factorial        // using previous        $fact = $fact * $x;        $x++;    }}     // Driver code    $n = 100;    echo printFactorialNums($n); // This code is contributed by ajit.?>
<script> // Javascript program to find all factorial numbers// smaller than or equal to n. function printFactorialNums(n){    let fact = 1;    let x = 2;    while (fact <= n) {        document.write(fact + " ");         // Compute next factorial        // using previous        fact = fact * x;         x++;    }} // Driver code     let n = 100;    printFactorialNums(n); // This code is contributed by Mayank Tyagi     </script>
Output:  
1 2 6 24
Time Complexity : O(n)If there are multiple queries, then we can cache all previously computed factorial numbers to avoid re-computations.This article is contributed by Shubham Sagar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
jit_t
mayanktyagi1709
factorial
Mathematical
Mathematical
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program for Decimal to Binary Conversion
Sieve of Eratosthenes
Find all factors of a natural number | Set 1
Program to find sum of elements in a given array
The Knight's tour problem | Backtracking-1
Operators in C / C++
Minimum number of jumps to reach end