Function to generate all primes
static ArrayList < Integer > SieveOfEratosthenes ( int n ) { boolean [ ] prime = new boolean [ 2 * n + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= 2 * n ; p ++ ) {