I'm trying to solve Problem 10 of ProjectEuler.net.
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
This is my Code:
/**
* Write a description of class ff here.
* Problem 010: 142913828922
*
* @author (your name)
* @version (a version number or a date)
*/
public class yy
{
public static void main (String args[])
{ int sum=0;
int limit=2000000;
boolean A[] = new boolean[limit];
A[0]=false;
A[1]=false;
for (int m=2;m<limit;m++)
{
A[m]=true;
}
for( int i=2 ; i<limit;i++)
{
if( A[i] == true)
{
for (int j=i+i; j<limit; j+=i)
{
A[j]=false;
}
}
}
for (int m=2 ;m<limit;m++)
{
if ( A[m] == true)
{ sum+=m; }
}
System.out.println(sum + "");
}
}
The algorithm is based on Sieve of Eratosthenes.I've tested it for values of limit 10,20 and it works, but when the input is the one from the problem ( two million ) the answer is wrong :bad:
Could someone please have a look the code, and tell me where is the bug :rolleyes:


Sign In
Create Account


Back to top










