-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject_euler_7.js
37 lines (31 loc) · 991 Bytes
/
project_euler_7.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
// Prime Numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, ...
function PrimeFinder(maxPrime) {
primes = [];
primes[0] = 2;
primes[1] = 3;
primeCount = 2; //2, and 3 are already listed as init primes
//Keep checking prime numbers until maxPrime is found
while (primeCount < maxPrime) {
isPrime = false; // Tracks if the current candidate is prime or not
candidate = primes[primeCount - 1] + 2; //contains variable that needs to be checked if it's prime or not
// While isPrime is false
while (!isPrime) {
isPrime = true;
for (var pindex = 0; pindex < primeCount; pindex++) {
prime = primes[pindex];
if (candidate % prime == 0) {
isPrime = false;
break;
}
}
if (!isPrime) {
candidate += 2;
} else {
primes[primeCount] = candidate;
primeCount++;
}
}
}
console.log( 'Solution: ' + primes[maxPrime - 1] ); // soultion is 104743
}
PrimeFinder(10001);