# An interesting solution to get all prime numbers smaller than n

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.

## C++

`// C++ program to Prints prime numbers smaller than n ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` `void` `primesInRange(` `int` `n) ` `{ ` ` ` `// Compute factorials and apply Wilson's ` ` ` `// theorem. ` ` ` `int` `fact = 1; ` ` ` `for` `(` `int` `k=2;k<n;k++){ ` ` ` `fact = fact * (k - 1); ` ` ` `if` `((fact + 1) % k == 0) ` ` ` `cout<<k<<endl; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 15; ` ` ` `primesInRange(n); ` ` ` `} ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Java

`// Java program prints prime numbers smaller than n ` `class` `GFG{ ` `static` `void` `primesInRange(` `int` `n) ` `{ ` ` ` `// Compute factorials and apply Wilson's ` ` ` `// theorem. ` ` ` `int` `fact = ` `1` `; ` ` ` `for` `(` `int` `k=` `2` `;k<n;k++){ ` ` ` `fact = fact * (k - ` `1` `); ` ` ` `if` `((fact + ` `1` `) % k == ` `0` `) ` ` ` `System.out.println(k); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args){ ` `int` `n = ` `15` `; ` `primesInRange(n); ` `} ` `} ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to prints prime numbers smaller than n ` `def` `primesInRange(n) : ` ` ` ` ` `# Compute factorials and apply Wilson's ` ` ` `# theorem. ` ` ` `fact ` `=` `1` ` ` `for` `k ` `in` `range` `(` `2` `, n): ` ` ` `fact ` `=` `fact ` `*` `(k ` `-` `1` `) ` ` ` `if` `((fact ` `+` `1` `) ` `%` `k ` `=` `=` `0` `): ` ` ` `print` `k ` ` ` `# Driver code ` `n ` `=` `15` `primesInRange(n) ` |

*chevron_right*

*filter_none*

## C#

`// C# program prints prime numbers smaller than n ` `class` `GFG{ ` `static` `void` `primesInRange(` `int` `n) ` `{ ` ` ` `// Compute factorials and apply Wilson's ` ` ` `// theorem. ` ` ` `int` `fact = 1; ` ` ` `for` `(` `int` `k=2;k<n;k++){ ` ` ` `fact = fact * (k - 1); ` ` ` `if` `((fact + 1) % k == 0) ` ` ` `System.Console.WriteLine(k); ` ` ` `} ` `} ` ` ` `// Driver code ` `static` `void` `Main(){ ` `int` `n = 15; ` `primesInRange(n); ` `} ` `} ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to prints prime numbers smaller than n ` `function` `primesInRange(` `$n` `) ` `{ ` ` ` `// Compute factorials and apply Wilson's ` ` ` `// theorem. ` ` ` `$fact` `= 1; ` ` ` `for` `(` `$k` `=2;` `$k` `<` `$n` `;` `$k` `++){ ` ` ` `$fact` `= ` `$fact` `* (` `$k` `- 1); ` ` ` `if` `((` `$fact` `+ 1) % ` `$k` `== 0) ` ` ` `print` `(` `$k` `.` `"\n"` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `$n` `= 15; ` `primesInRange(` `$n` `); ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

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 contribute@geeksforgeeks.org. 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.

## Recommended Posts:

- 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
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Interesting facts about Fibonacci numbers
- Print the nearest prime number formed by adding prime numbers to N
- Check if a prime number can be expressed as sum of two Prime Numbers
- Print prime numbers with prime sum of digits in an array
- Generate a list of n consecutive composite numbers (An interesting method)
- Minimum numbers (smaller than or equal to N) with sum S
- Cube Free Numbers smaller than n
- Print all Jumping Numbers smaller than or equal to a given value
- Count of Binary Digit numbers smaller than N
- Number of n digit stepping numbers | Space optimized solution
- 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