So I modified a program to search for the 10001th prime, and it won't return. Here's the code:
Code:
I run it with:
Code:
Any ideas? NOTE: I didn't comment 'cause it's pretty self-explanitory. Please ask if you don't get it. And I DON'T want the answer.
Code:
import java.util.*;
public class MathUtil{
public static Long gcd(Long x, Long y){
while(x != y){
if(x> y){
x-= y;
}
else{
y-= x;
}
}
return x;
}
public static Long lcm(Long x, Long y){
x= Math.abs(x);
y= Math.abs(y);
while(MathUtil.gcd(x, y)!=1) x= MathUtil.gcd(x, y);
return x*y;
}
public static TreeSet<Long> getPrimes(int length){
TreeSet<Long> primes= new TreeSet<Long>();
primes.add(new Long(2));
for(Long i=new Long(3); i<length; i++){
boolean save= true;
for(Long j: primes){
if(MathUtil.gcd(i,j)==1){
continue;
}
else{
save= false;
break;
}
}
if(save) primes.add(i);
}
return primes;
}
}
I run it with:
Code:
import java.util.TreeSet;
public class test {
public static void main(String[] args) {
TreeSet<Long> output= MathUtil.getPrimes(10001);
System.out.println(output.last());
}
}
Any ideas? NOTE: I didn't comment 'cause it's pretty self-explanitory. Please ask if you don't get it. And I DON'T want the answer.