I'm trying to find how many prime number I can have until the product of biggest two is over Long.MAX_VALUE.
It's taking more than half an hour (and GBs of RAM)
public class Main {
public static void main(String[] args) {
ArrayList <Long> primes= new ArrayList<Long>();
primes.add(2L);
long i=3L;
// Looping from 3, to the limit
while (primes.size()<2||(primes.get(primes.size()-1)*primes.get(primes.size()-2)<Long.MAX_VALUE)) {
boolean isPrime = true;
long maxDiv =Math.round(Math.sqrt(i));
int j=0;
while(primes.get(j)<maxDiv && isPrime) {
if (i % primes.get(j) == 0) {
isPrime = false;
}
j++;
}
if (isPrime) {
primes.add(i);
System.out.println(i);
}
i=i+2;
}
System.out.println("max size is: "+primes.size());
}
}
EDIT
I'm also interested in how many prime numbers I get before reaching this limit. So a top-down approach wouldn't do the job.
Anyway, I realized that I would be able to reach those two number in my application, I would have become rich as hell in the meantime :)