// This program maintains an array of the first N primes and uses it to
// compute the next prime number.  The primes are printed in a table
// with 8 columns.

import java.util.Scanner;

public class PrimeList {
	private int[] Primes;
	private int size;
	
	public PrimeList(int maxsize) {
		size = 0;
		Primes = new int[maxsize];
	}
	
	public boolean isPrime(int n) {
		for (int i =0; i < size; i++ ) {
			int p = Primes[i];
			if (p*p > n)
				break;
			if (n%p == 0)
				return false;
		}
		return true;
	}
	
	public void FindPrimes() {
		int p = 2;
		while (size < Primes.length) {
			if (isPrime(p)) {
				Primes[size]=p;
				size += 1;
			}
			if (p == 2)
				p = 3;
			else
				p += 2;
		}
	}
	
	public void PrintPrimes() {
		int lineCount = 0;
		for (int i = 0; i < Primes.length; i++) {
			System.out.printf( "%10d", Primes[i]);
			lineCount += 1;
			if (lineCount %8 == 0) 
				System.out.println();
		}
	}
	
	public static void main(String[] args) {
		Scanner console = new Scanner(System.in);
		System.out.print( "How many primes? ");
		int n = console.nextInt();
		PrimeList p = new PrimeList(n);
		p.FindPrimes();
		p.PrintPrimes();
	}

}
