Given a number n, the task is to determine whether n can be a factorial of some number x
Examples:
Input: N = 24 Output: Yes Explanation: 4! = 24 Input: N = 25 Output: No
Approach:
-
Divide x by 2, divide by 3, and so on, until you cannot divide further. If you reach the number 1, then it’s factorial number else not.
Below is the implementation of the above approach:
C++
// C++ implementation for // the above approach #include <bits/stdc++.h> using namespace std; // Function to check if // the given number is a // factorial of any number bool isFactorial(int n) { for (int i = 1;; i++) { if (n % i == 0) { n /= i; } else { break; } } if (n == 1) { return true; } else { return false; } } // Driver Code int main() { int n = 24; bool ans = isFactorial(n); if (ans == 1) { cout << "Yes\n"; } else { cout << "No\n"; } return 0; } |
chevron_right
filter_none
Java
// Java implementation for the above approach class GFG { // Function to check if the given number // is a factorial of any number static boolean isFactorial(int n) { for (int i = 1;; i++) { if (n % i == 0) { n /= i; } else { break; } } if (n == 1) { return true; } else { return false; } } // Driver Code public static void main (String[] args) { int n = 24; boolean ans = isFactorial(n); if (ans == true) { System.out.println("Yes"); } else { System.out.println("No"); } } } // This code is contributed by AnkitRai01 |
chevron_right
filter_none
Python3
# Python3 implementation of the approach # Function to check if # the given number is a # factorial of any number def isFactorial(n) : i = 1; while(True) : if (n % i == 0) : n //= i; else : break; i += 1; if (n == 1) : return True; else : return False; # Driver Code if __name__ == "__main__" : n = 24; ans = isFactorial(n); if (ans == 1) : print("Yes"); else : print("No"); # This code is contributed by kanugargng |
chevron_right
filter_none
C#
// C# implementation for the above approach using System; class GFG { // Function to check if the given number // is a factorial of any number static Boolean isFactorial(int n) { for (int i = 1;; i++) { if (n % i == 0) { n /= i; } else { break; } } if (n == 1) { return true; } else { return false; } } // Driver Code public static void Main (String[] args) { int n = 24; Boolean ans = isFactorial(n); if (ans == true) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } } // This code is contributed by 29AjayKumar |
chevron_right
filter_none
Output:
Yes
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.
Recommended Posts:
- Find the last digit when factorial of A divides factorial of B
- Find the length of factorial of a number in any given base
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Check if any permutation of N equals any power of K
- Check if a subarray of length K with sum equal to factorial of a number exists or not
- Check if a given number can be represented in given a no. of digits in any base
- Check if given array can be made 0 with given operations performed any number of times
- Check if N is a Factorial Prime
- 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
- Check if N-factorial is divisible by X^Y
- Check if the remainder of N-1 factorial when divided by N is N-1 or not
- Calculate MDAS Factorial of given number
- Find the last two digits of Factorial of a given Number
- Count factorial numbers in a given range
- Find GCD of factorial of elements of given array
- Check if X and Y can be made zero by using given operation any number of times
- Count trailing zeroes in factorial of a number
- Factorial of a large number
- Find the first natural number whose factorial is divisible by x
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

