Skip to content

Commit 1596012

Browse files
committed
added more comments and standardized code
1 parent 11dcd8d commit 1596012

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

Project-Euler/Problem7.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
// https://projecteuler.net/problem=7
2+
// My approach does not use the Sieve of Eratosthenes but that is another common way to approach this problem. Sieve of Atkin is another possibility as well.
23

3-
const num = 10001;
4-
5-
let primes = [2,3,5,7,11,13]; // given list of primes you start with
4+
const num = 10001
5+
const primes = [2, 3, 5, 7, 11, 13] // given list of primes you start with
66

77
const calculatePrime = (num) => {
8-
let count = primes.length; // count number of primes calculated
9-
let current = primes[count-1] + 1; // current number being assessed if prime
8+
// Calculate each next prime by checking each number to see what it's divisible by
9+
let count = primes.length // count number of primes calculated
10+
let current = primes[count - 1] + 1 // current number being assessed if prime
1011
while (count < num) { // repeat while we haven't reached goal number of primes
1112
// go through each prime and see if divisible by the previous primes
12-
let prime = false;
13+
let prime = false
1314
primes.some((n, i) => {
1415
if (current % n === 0) {
15-
return true;
16+
return true
1617
}
17-
if (i === count-1) {
18-
prime = true;
18+
if (i === count - 1) {
19+
prime = true
1920
}
2021
})
21-
if (prime) {
22-
primes.push(current);
23-
count += 1;
22+
if (prime) { // if prime, add to prime list and increment count
23+
primes.push(current)
24+
count += 1
2425
}
25-
current += 1;
26+
current += 1
2627
}
27-
return primes[num-1];
28+
return primes[num - 1]
2829
}
2930

30-
console.log(calculatePrime(num));
31+
console.log(calculatePrime(num))

0 commit comments

Comments
 (0)