Skip to content

Commit 41fc82c

Browse files
authored
Merge pull request #472 from victorialo/eulerProblem7
Euler Problem 7
2 parents 6c47ed9 + 1596012 commit 41fc82c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Project-Euler/Problem7.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// 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.
3+
4+
const num = 10001
5+
const primes = [2, 3, 5, 7, 11, 13] // given list of primes you start with
6+
7+
const calculatePrime = (num) => {
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
11+
while (count < num) { // repeat while we haven't reached goal number of primes
12+
// go through each prime and see if divisible by the previous primes
13+
let prime = false
14+
primes.some((n, i) => {
15+
if (current % n === 0) {
16+
return true
17+
}
18+
if (i === count - 1) {
19+
prime = true
20+
}
21+
})
22+
if (prime) { // if prime, add to prime list and increment count
23+
primes.push(current)
24+
count += 1
25+
}
26+
current += 1
27+
}
28+
return primes[num - 1]
29+
}
30+
31+
console.log(calculatePrime(num))

0 commit comments

Comments
 (0)