Sunday, October 12, 2014

10001st prime

Ques:
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?
 
 
Ans:
Prime number is number divided by 1 or itself. 
1st Prime=2 is fixed. 2nd onwards we calculate as below........
============================================================
package isprime;

public class IsPrime {

        public static void main(String[] args) {
        int count = 0;
        int pcount=1;//counts Prime
        int tarprnum=10001;//target expected to be met
        for (int i = 3; i < 1000000000; i++) {
            count=0;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    count++;
                    break;
                }
            }
            if(count==0){
                pcount++;
                    }
            if(pcount==tarprnum){
          System.out.println("The target prime num="+i);break;}
        }
    }

}

No comments:

Post a Comment