-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path30-CoderByte-Prime-Mover.js
More file actions
35 lines (28 loc) · 1.03 KB
/
Copy path30-CoderByte-Prime-Mover.js
File metadata and controls
35 lines (28 loc) · 1.03 KB
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
//Prime Mover
//Using the JavaScript language, have the function PrimeMover(num) return the numth prime number.
//The range will be from 1 to 10^4. For example: if num is 16 the output should be 53 as 53 is the 16th prime number.
//Used the function from the other problem to check for prime numbers
function PrimeTime(number) {
if(number<2 || number != Math.round(number)){
return false;
}
for(var i = 2; i < number; i++){
if(number%i == 0){
return false;
}
}
return true;
};
function PrimeMover(num) {
var primeArray = []; //Stores all the prime numbers
if(num == 0){ //Initial check
return 1;
}
for(var i = 1; i < 100000; i++){ //Loops from 1 to 100000 checking for prime numbers. If there is one then it
if(PrimeTime(i)){ //If there is one then it gets pushed into the array.
primeArray.push(i);
}
}
return primeArray[num]; //returns the nth element of the array with num.
};
//If you store all the prime numbers in an array then num is equal to the nth element of that array