Skip to content

Commit d90e6d2

Browse files
committed
code for problem 7 works
1 parent 89df465 commit d90e6d2

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+
3+
const num = 10001;
4+
5+
let primes = [2,3,5,7,11,13];
6+
7+
const calculatePrime = (num) => {
8+
let count = primes.length;
9+
let current = primes[count-1] + 1;
10+
while (count < num) {
11+
// go through each prime and see if divisible by the previous primes
12+
let prime = false;
13+
primes.some((n, i) => {
14+
if (current % n !== 0) {
15+
if (i === count-1) {
16+
prime = true;
17+
}
18+
} else {
19+
return true;
20+
}
21+
})
22+
if (prime) {
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)