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:
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.
- Find the last digit when factorial of A divides factorial of B
- Largest power of k in n! (factorial) where k may not be 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 a given number is factorial of any number
- 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
- Check if a subarray of length K with sum equal to factorial of a number exists or not
- Quick ways to check for Prime and find next Prime in Java
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check whether the sum of prime elements of the array is prime or not
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Count digits in a factorial | Set 2
- Count trailing zeroes in factorial of a number
- Factorial of a large number
- Count factorial numbers in a given range
- Count digits in a factorial | Set 1
- Count Divisors of Factorial
- Find the first natural number whose factorial is divisible by x
- Smallest number with at least n trailing zeroes in factorial
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.