package com.translationdata.p010;
/** Strategy: Brute Force. */
import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import JUnitTests.FastTest;
import JUnitTests.SlowTest;
public class P010_SummationOfPrimes {
	static int testNo = 1;

	public long primesSum(final int upperLimit) {
		int primeCandidate;
		int primeCount;
		long primeSum = 2;
		long primes[] = new long[200_000];
		
		primes[0] = 2;
		primeCount = 1;
		primeCandidate = 3;
		
		while (primeCandidate < upperLimit) {
			boolean isPrime = true;		
			for(int i = 0; i < primeCount; i++) {
				if ( isRemainderOfDivisionZero(primeCandidate, primes[i]) ) {
					isPrime = false;
					break;
				}
			}

			if (isPrime == true) {
				primeSum += primeCandidate;
				try {
					primes[primeCount] = primeCandidate;
				} catch (Exception e) {
					e.printStackTrace();
					System.out.printf("sum = %d\n", primeSum);
				}
				primeCount += 1;
			}
			primeCandidate = nextCandidate(primeCandidate);
		}
		System.out.printf("Result(%d) = %d, no. of primes %d%n", upperLimit, primeSum, primeCount);
		return primeSum;
	}

	private int nextCandidate(final int candidate) {
		return candidate + 2;
	}
	private boolean isRemainderOfDivisionZero (final int numerator, final long denominator) {
		return numerator % denominator == 0;
	}

	@Before
	public void before() {
		System.out.printf("Running unit test...%d%n", testNo);
		testNo += 1;
	}
	
	@Test(timeout = 300_000)
	@Category(SlowTest.class)
	public void TwoMillionPrimes() {
		// takes ~133 seconds on an i7 laptop
		long primeSum = primesSum(2_000_000);
		assertEquals( "Incorrect sum", 142913828922L, primeSum );

	}
	
	@Test(timeout = 500)
	@Category(FastTest.class)
	public void SixPrimes() {
		long primeSum = primesSum(6);
		assertEquals( "Incorrect sum", 10, primeSum );
	}
	
	@Test(timeout = 500)
	@Category(FastTest.class)
	public void TenPrimes() {
		long primeSum = primesSum(10);
		assertEquals("Incorrect sum", 17, primeSum);
	}
}

@@ -258,16 +258,50 @@
 lowTest;
+%0A%0Aimport misc.SieveOfEratosthenes;
 %0Apublic 
@@ -470,949 +470,123 @@
 ;%0A%09%09
-long primes%5B%5D = new long%5B200_000%5D;%0A%09%09%0A%09%09primes%5B0%5D = 2;%0A%09%09primeCount = 1;%0A%09%09primeCandidate = 3;%0A%09%09%0A%09%09while (primeCandidate %3C upperLimit) %7B%0A%09%09%09boolean isPrime = true;%09%09%0A%09%09%09for(int i = 0; i %3C primeCount; i++) %7B%0A%09%09%09%09if ( isRemainderOfDivisionZero(primeCandidate, primes%5Bi%5D) ) %7B%0A%09%09%09%09%09isPrime = false;%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%0A%09%09%09if (isPrime == true) %7B%0A%09%09%09%09primeSum += primeCandidate;%0A%09%09%09%09try %7B%0A%09%09%09%09%09primes%5BprimeCount%5D = primeCandidate;%0A%09%09%09%09%7D catch (Exception e) %7B%0A%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%09System.out.printf(%22sum = %25d%5Cn%22, primeSum);%0A%09%09%09%09%7D%0A%09%09%09%09primeCount += 1;%0A%09%09%09%7D%0A%09%09%09primeCandidate = nextCandidate(primeCandidate);%0A%09%09%7D%0A%09%09System.out.printf(%22Result(%25d) = %25d, no. of primes %25d%25n%22, upperLimit, primeSum, primeCount);%0A%09%09return primeSum;%0A%09%7D%0A%0A%09private int nextCandidate(final int candidate) %7B%0A%09%09return candidate + 2;%0A%09%7D%0A%09private boolean isRemainderOfDivisionZero (final int numerator, final long denominator) %7B%0A%09%09return numerator %25 denominator == 0
+%0A%09%09%0A%09%09System.out.printf(%22Result(%25d) = %25d, no. of primes %25d%25n%22, upperLimit, primeSum, primeCount);%0A%09%09return primeSum
 ;%0A%09%7D
@@ -929,331 +929,8 @@
 );%0A%0A
-%09%7D%0A%09%0A%09@Test(timeout = 500)%0A%09@Category(FastTest.class)%0A%09public void SixPrimes() %7B%0A%09%09long primeSum = primesSum(6);%0A%09%09assertEquals( %22Incorrect sum%22, 10, primeSum );%0A%09%7D%0A%09%0A%09@Test(timeout = 500)%0A%09@Category(FastTest.class)%0A%09public void TenPrimes() %7B%0A%09%09long primeSum = primesSum(10);%0A%09%09assertEquals(%22Incorrect sum%22, 17, primeSum);%0A
 %09%7D%0A%7D

Delete original code.