Hello DaniWeb community! I am writing code for problem 10 of Project Euler. I have decided that the Sieve of Eratosthenes was the best option for creating a list of primes less than two million. Here is my code (yes, I mis-spelled Sieve and Eratosthenes):
package projecteulerjava;
class ProblemTen
{
public ProblemTen()
{
boolean Primes[] = Erasthones(2000000);
int Total = 0;
for (int i = 2; i < Primes.length; i ++)
{
if (!Primes[i])
{
boolean IsPrime = Primes[i];
Total += i;
}
}
System.out.println("The sum of the primes up to two million is " + Total);
}
public static boolean[] Erasthones(int ToWhat)
{
boolean Seive[] = new boolean[ToWhat + 1];
int PrimeAt = 2;
int Multiplier = 2;
while (PrimeAt <= ToWhat)
{
while ((Multiplier * PrimeAt) < (ToWhat - 1))
{
Seive[Multiplier * PrimeAt] = true;
Multiplier ++;
}
Multiplier = 2;
for (int i = PrimeAt + 1; i < Seive.length; i ++)
{
if (Seive[i] == false)
{
PrimeAt = i;
break;
}
}
if (PrimeAt == ToWhat)
{
break;
}
}
return Seive;
}
}
Okay. So I ran this code -- first time I got a promising result. Apparently -- and I have done research -- the result it gives me, 1183908153, is wrong. I immediately turned towards the debugger (using NetBeans 7.0). Upon close investigation, my Sieve is working perfectly, which leads to an interesting question: How was my summing method wrong? TO THE DEBUGGER! As soon as I turned the debugger on and set its breakpoints in the for loop to sum, it started to work perfectly, so we now are at my predicament -- it works with the debugger, and does not without it. Needles to say, I am awfully confused and would greatly appreciate any help you could give me.
EneilShade