Given a positive integer N, the task is to check if N is a Factorial prime or not. If it is a factorial prime then print YES else print NO.
Input: N = 23
23 is a prime number and one less than factorial of 4 (4! = 24).
11 is a prime number but can not be expressed as either n! + 1 or n! – 1.
Approach: In order for N to be factorial number, N must be a prime and either N – 1 or N + 1 should be the value of factorial of any number.
- If N is not prime then print No.
- Else set fact = 1 and starting from i = 1 update fact = fact * i, if fact = N – 1 or fact = N + 1 then print Yes.
- Repeat the above step until fact ≤ N + 1 and if the condition is not satisfied then print No in the end.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Largest power of k in n! (factorial) where k may not be prime
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Quick ways to check for Prime and find next Prime in Java
- Check if N-factorial is divisible by X^Y
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check whether factorial of N is divisible by sum of first N natural numbers
- Check whether the sum of prime elements of the array is prime or not
- Check if factorial of N is divisible by the sum of squares of first N natural numbers
- Find the last digit when factorial of A divides factorial of B
- Check n^2 - m^2 is prime or not
- Check if two numbers are co-prime or not
- Check if N is Strong Prime
- Check if a number is Full Prime
- Check whether N is a Dihedral Prime Number or not
- Check if a number is a Pythagorean Prime or not
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.