
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if N is a Factorial Prime in Python
Suppose we have a number n, we have to check whether n is a Factorial prime or not. As we know a number is said to be a factorial prime when it is a prime number that is one less than or one more than a factorial of any number.
So, if the input is like n = 719, then the output will be True as 719 = 720 - 1 = 6! - 1
To solve this, we will follow these steps −
- if num is not a prime, then
- return False
- factorial := 1, i := 1
- while factorial <= num + 1, do
- factorial := factorial * i
- if num + 1 is same as factorial or num - 1 is same as factorial, then
- return True
- i := i + 1
- return False
Example
Let us see the following implementation to get better understanding −
from math import sqrt def isPrime(num) : if num <= 1: return False if num <= 3 : return True if num % 2 == 0 or num % 3 == 0: return False for i in range(5, int(sqrt(num)) + 1, 6) : if num % i == 0 or num % (i + 2) == 0: return False return True def solve(num) : if not isPrime(num) : return False factorial = 1 i = 1 while factorial <= num + 1: factorial *= i if num + 1 == factorial or num - 1 == factorial : return True i += 1 return False num = 719 print(solve(num))
Input
719
Output
True
- Related Questions & Answers
- Check if N is Strong Prime in Python
- Check if a number is Primorial Prime or not in Python
- Check whether N is a Dihedral Prime Number or not in Python
- Python program to check if a number is Prime or not
- Check if a number is Full Prime in C++
- Check if difference of areas of two squares is prime in Python
- Check if product of array containing prime numbers is a perfect square in Python
- Check if a number is Quartan Prime or not in C++
- Check if a number is Primorial Prime or not in C++
- Check if a number is a Pythagorean Prime or not in C++
- C# Program to check if a number is prime or not
- PHP program to check if a number is prime or not
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- Check if a string follows a^n b^n pattern or not in Python
- Check if LCM of array elements is divisible by a prime number or not in Python
Advertisements