A number N is called a factorial number if it is the factorial of a positive integer. For example, the first few factorial numbers are
1, 2, 6, 24, 120, …
Given a number n, print all factorial numbers smaller than or equal to n.
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.
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 contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print all Strong numbers less than or equal to N
- Find the last digit when factorial of A divides factorial of B
- Find the next Factorial greater than N
- Count factorial numbers in a given range
- Expressing factorial n as sum of consecutive numbers
- Check whether factorial of N is divisible by sum of first N natural numbers
- Check if factorial of N is divisible by the sum of squares of first N natural numbers
- Factorial of Large numbers using Logarithmic identity
- Find the first natural number whose factorial is divisible by x
- Find sum of digits in factorial of a number
- Find maximum power of a number that divides a factorial
- Find GCD of factorial of elements of given array
- Find the length of factorial of a number in any given base
- Find the last two digits of Factorial of a given Number
- Find last digit in factorial
- An interesting solution to get all prime numbers smaller than n
- Count digits in a factorial | Set 2
- Count trailing zeroes in factorial of a number
- Factorial of a large number
- Count digits in a factorial | Set 1