Your algorithm is going to be extremely slow. I would look into using Euler's Sieve (Wikipedia has listed it in the Sieve of Eratosthenes.)
To keep the sum of a prime number in your code, just have a variable outside the loops and add
i
to it every time
i == j
. For a mathematical solution, try this:
Prime Sums -- from Wolfram MathWorld[
^]