Problem 46: What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
import java.util.Vector;
class runner
{
public static boolean isPrime(int n){
if(primes.contains(n)) return true;
double limit = Math.sqrt(n);
for(int p: primes){
if(p > limit) break;
if(n % p == 0) return false;
}
primes.add(n);
return true;
}
static Vector primes = new Vector();
public static void main (String[] args) throws java.lang.Exception
{
long time = System.currentTimeMillis();
primes.add(2);
int n = 3;
outerLoop:
while(true){
if(!isPrime(n) && n % 2 == 1){
boolean found = false;
for(int p: primes){
if(p < n-1){
double sqrtdiff = Math.sqrt((n-p)/2.0);
if(Math.floor(sqrtdiff) == sqrtdiff){
found = true;
}
}
}
if(!found){
break outerLoop;
}
}
n++;
}
System.out.println("n: "+n);
System.out.println("time: "+(System.currentTimeMillis() - time));
}
}