// Arup Guha
// 9/14/2014
// Prime Sieve Example

import java.util.*;

public class sieve {
	
	final public static int MAX = 10000000;
	final public static boolean DEBUG = false;
	
	public static void main(String[] args) {
		
		// Basic version
		boolean[] isPrime = new boolean[MAX];
		Arrays.fill(isPrime, true);
		isPrime[0] = false;
		isPrime[1] = false;
		
		for (int i=2; i<MAX; i++)
			for (int j=2*i; j<MAX; j+=i)
				isPrime[j] = false;
				
		// Just print primes.
		if (DEBUG) {
			for (int i=0; i<MAX; i++)
				if (isPrime[i])
					System.out.print(i+" ");
			System.out.println();
		}
		
		// Slightly faster version
		Arrays.fill(isPrime, true);
		isPrime[0] = false;
		isPrime[1] = false;		
		
		// We stop at the square, and don't redivide for composites.
		for (int i=2; i<Math.sqrt(MAX)+1; i++)
			if (isPrime[i])
				for (int j=2*i; j<MAX; j+=i)
					isPrime[j] = false;	
						
		// Just print primes.
		if (DEBUG) {
			for (int i=0; i<MAX; i++)
				if (isPrime[i])
					System.out.print(i+" ");
			System.out.println();
		}
		
		// Count primes.
		int numprimes = 0;
		for (int i=0; i<MAX; i++)
			if (isPrime[i])
				numprimes++;
				
		// Allocate space and copy them over.
		int[] primelist = new int[numprimes];
		int j = 0;
		for (int i=0; i<MAX; i++)
			if (isPrime[i])
				primelist[j++] = i;		

		// See if it worked!
		if (DEBUG) {
			for (int i=0; i<primelist.length; i++)
				System.out.print(primelist[i]+" ");
			System.out.println();
		}	
			
		// Always print out the total number of primes in range.
		System.out.println("There are "+primelist.length+" primes upto "+(MAX-1));						
	}
}