-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path007.js
48 lines (38 loc) · 1.32 KB
/
007.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
48
/*
Source: https://projecteuler.net/problem=7
Problem:
"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?"
My Algorithm:
1. Define a function to check if a number is prime
2. Define an array to store prime numbers in
3. Use a variable called "i" to generate prime numbers until i
4. Use a while loop to keep generating prime numbers until 10,001 have been generated
5. Return the 10001th value of the array (index [10001-1] because arrays start at index 0)
*/
function isPrime(n) {
if (n === 1) {
return false; //1 isn't prime
} else if (n === 2) {
return true; //2 is prime
} else {
for (var i = 2; i < n; i++) {
if (n % i === 0) {
return false; //if any number from i to n is divisible by i, the function immediately stops and returns false
}
}
return true;
}
}
function nthPrimeNumber(n) {
let primes = [];
let i = 1;
while (primes.length < n) { //while the array doesn't have n prime numbers in it...
i++; //increment i
if (isPrime(i)) { //check if i is prime
primes.push(i) //if so, add to primes array
}
}
return primes[n-1]; //Return the nth value of the array (index [n-1] because arrays start at index 0)
}
console.log(nthPrimeNumber(10001)); //outputs the answer!