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