I want to add prime numbers under two million, and for some reason it keeps running with no output. After some testing I know that the error itself is in the the addprimes
function however from what I understand the program should return an output.
static ArrayList<Integer> primes = new ArrayList<Integer>();
public static void findPrimes(int limit)
{
primes.add(2);
int Number = 3;
int n = 0;
boolean add = true;
if(Number % 2 != 0)
primes.add(Number);
for(int i = Number; i<limit; i+=2)
{
n = 2;
add = true;
while(n < i)
{
if(i % n == 0)
{
add = false;
continue;
}
n++;
}
if(add == true)
primes.add(i);
}
}
public static int addPrimes()
{
int Total = 0;
int temp = 0;
for(int i = 0; i < primes.size(); i++)
{
temp = primes.get(i);
Total += temp;
}
return Total;
}
}