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