This approach is based on Wilson’s theorem and using the fact that factorial computation can be done easily using DP
Wilson theorem says if a number k is prime then ((k-1)! + 1) % k must be 0.
Below is Python implementation of the approach. Note that the solution works in Python because Python supports large integers by default therefore factorial of large numbers can be computed.
2 3 5 7 11 13
This article is contributed by Parikshit Mukherjee. 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 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.
- Find initial integral solution of Linear Diophantine equation if finite solution exists
- Print all Jumping Numbers smaller than or equal to a given value
- Euler's Totient function for all numbers smaller than or equal to n
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Count of Binary Digit numbers smaller than N
- Cube Free Numbers smaller than n
- Minimum numbers (smaller than or equal to N) with sum S
- Sieve of Sundaram to print all primes smaller than n
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Numbers less than N which are product of exactly two distinct prime numbers
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Count all prime numbers in a given range whose sum of digits is also prime
- Print all prime numbers less than or equal to N
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Print all Semi-Prime Numbers less than or equal to N
- Largest number smaller than or equal to n and digits in non-decreasing order
- Maximize a number considering permutations with values smaller than limit