
import java.io.PrintStream;

/**
 * brute force search for factors
 *
 * ported from http://www.troubleshooters.com/codecorn/primenumbers/primenumbers.htm
 *
 * Created by Xan Gregg.
 * Date: Nov 13, 2004
 */
public class FindPrimes1 {

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        //long count = findPrimes(10000, System.out);
        long count = findPrimes(10000000, new PrintStream(new NullOutputStream()));
        long end = System.currentTimeMillis();
        long elapsed = end - start;
        System.out.println("Primes: " + count + "; seconds: " + elapsed / 1000.0);
    }

    public static long findPrimes(long topCandidate, PrintStream out) {
        long count = 0;
        long candidate = 2;
        while (candidate <= topCandidate) {
            long trialDivisor = 2;
            boolean prime = true;
            while (trialDivisor * trialDivisor <= candidate) {
                if (candidate % trialDivisor == 0) {
                    prime = false;
                    break;
                }
                trialDivisor++;
            }
            if (prime) {
                out.println(candidate);
                count ++;
            }
            candidate++;
        }
        return count;
    }
}
