What is the 10 001st prime number?
Solution:
not the best solution, Use sieve of atkins or eratosthenes to faster solution.
Java code:
public class Prob7 { public static void main(String[] args) { int k= 1 ; int i =3; while(k!=10001) { if(checkPrime(i)) { k++; } i++; } System.out.println(i-1); } public static boolean checkPrime(int a) { boolean flag = true; for(int i = 2 ; i < a ; i ++ ) { if(a % i == 0 ) { flag = false; break; } } return flag; } }
No comments:
Post a Comment