PROBLEM 7
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
ANSWER
104743
JAVA CODE
public class Problem7 {
public static void main(String[]args){
primeGenerator();
}
private static void primeGenerator() {
int max=775146;
int primeCount=0;
int ans=0;
for (int j = 2; j < max; j++) {
boolean prime=true;
for (int i = 2; i <= (int)Math.sqrt(j); i++) {
if(j%i==0 )
prime=false;
}
if(prime){
primeCount++;
ans=j;
}
if(primeCount>=10001)
break;
}
System.out.println("ans="+ans);
}
}
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
ANSWER
104743
JAVA CODE
public class Problem7 {
public static void main(String[]args){
primeGenerator();
}
private static void primeGenerator() {
int max=775146;
int primeCount=0;
int ans=0;
for (int j = 2; j < max; j++) {
boolean prime=true;
for (int i = 2; i <= (int)Math.sqrt(j); i++) {
if(j%i==0 )
prime=false;
}
if(prime){
primeCount++;
ans=j;
}
if(primeCount>=10001)
break;
}
System.out.println("ans="+ans);
}
}
0 comments:
Post a Comment