-
Notifications
You must be signed in to change notification settings - Fork 0
/
nthPrime.js
48 lines (43 loc) · 1010 Bytes
/
nthPrime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//10001st Prime numbers...
// const readline = require("readline");
// const r1 = readline.createInterface({
// input: process.stdin,
// output: process.stdout
// });
// r1.question("which prime number is required?",function(value1){
// var value3 = nthPrime(value1);
// console.log("value of ${value1}th Prime is "+ value3);
// r1.close();
// });
console.log("Value of 10001st Prime is "+nthPrime(10001));
function nthPrime(value1){
var n=1;
var i=1;
while(n<=value1){
if (getPrime(i) == 'Yes') {
if (n == value1){
var value2 = i;
}
n++;
}
i++;
if (i > 999999) {
console.log ("am in infinite looop");
break;
}
}
return value2;
}
function getPrime(i){
var prime = 'no'
var count=0;
for (j=1;j<=i;j++){
if (i%j == 0) {
count++;
}
}
if (count ==2) {
prime = 'Yes';
}
return prime;
}