Java implementation of above approach
import java . util . * ; class GfG { static boolean prime [ ] = new boolean [ 1000000 + 5 ] ; static void findPrime ( ) { Arrays . fill ( prime , true ) ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= 1000000 ; p ++ ) {