From d90e6d2e2d1495c258a7cf16def4933726833c15 Mon Sep 17 00:00:00 2001 From: Victoria Lo Date: Thu, 15 Oct 2020 03:45:14 -0700 Subject: [PATCH 1/3] code for problem 7 works --- Project-Euler/Problem7.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Project-Euler/Problem7.js diff --git a/Project-Euler/Problem7.js b/Project-Euler/Problem7.js new file mode 100644 index 0000000000..f68212512c --- /dev/null +++ b/Project-Euler/Problem7.js @@ -0,0 +1,31 @@ +// https://projecteuler.net/problem=7 + +const num = 10001; + +let primes = [2,3,5,7,11,13]; + +const calculatePrime = (num) => { + let count = primes.length; + let current = primes[count-1] + 1; + while (count < num) { + // go through each prime and see if divisible by the previous primes + let prime = false; + primes.some((n, i) => { + if (current % n !== 0) { + if (i === count-1) { + prime = true; + } + } else { + return true; + } + }) + if (prime) { + primes.push(current); + count += 1; + } + current += 1; + } + return primes[num-1]; +} + +console.log(calculatePrime(num)); \ No newline at end of file From 11dcd8d22b9eb882dfdb9db04421b6cca91ff458 Mon Sep 17 00:00:00 2001 From: Victoria Lo Date: Thu, 15 Oct 2020 04:33:10 -0700 Subject: [PATCH 2/3] minor refactor and comments --- Project-Euler/Problem7.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Project-Euler/Problem7.js b/Project-Euler/Problem7.js index f68212512c..c841c33a0f 100644 --- a/Project-Euler/Problem7.js +++ b/Project-Euler/Problem7.js @@ -2,22 +2,21 @@ const num = 10001; -let primes = [2,3,5,7,11,13]; +let primes = [2,3,5,7,11,13]; // given list of primes you start with const calculatePrime = (num) => { - let count = primes.length; - let current = primes[count-1] + 1; - while (count < num) { + let count = primes.length; // count number of primes calculated + let current = primes[count-1] + 1; // current number being assessed if prime + while (count < num) { // repeat while we haven't reached goal number of primes // go through each prime and see if divisible by the previous primes let prime = false; primes.some((n, i) => { - if (current % n !== 0) { - if (i === count-1) { - prime = true; - } - } else { + if (current % n === 0) { return true; } + if (i === count-1) { + prime = true; + } }) if (prime) { primes.push(current); From 1596012d829b9488d172c2b42000181f186d5e64 Mon Sep 17 00:00:00 2001 From: Victoria Lo Date: Thu, 15 Oct 2020 04:46:54 -0700 Subject: [PATCH 3/3] added more comments and standardized code --- Project-Euler/Problem7.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/Project-Euler/Problem7.js b/Project-Euler/Problem7.js index c841c33a0f..debc75276b 100644 --- a/Project-Euler/Problem7.js +++ b/Project-Euler/Problem7.js @@ -1,30 +1,31 @@ // https://projecteuler.net/problem=7 +// 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. -const num = 10001; - -let primes = [2,3,5,7,11,13]; // given list of primes you start with +const num = 10001 +const primes = [2, 3, 5, 7, 11, 13] // given list of primes you start with const calculatePrime = (num) => { - let count = primes.length; // count number of primes calculated - let current = primes[count-1] + 1; // current number being assessed if prime + // Calculate each next prime by checking each number to see what it's divisible by + let count = primes.length // count number of primes calculated + let current = primes[count - 1] + 1 // current number being assessed if prime while (count < num) { // repeat while we haven't reached goal number of primes // go through each prime and see if divisible by the previous primes - let prime = false; + let prime = false primes.some((n, i) => { if (current % n === 0) { - return true; + return true } - if (i === count-1) { - prime = true; + if (i === count - 1) { + prime = true } }) - if (prime) { - primes.push(current); - count += 1; + if (prime) { // if prime, add to prime list and increment count + primes.push(current) + count += 1 } - current += 1; + current += 1 } - return primes[num-1]; + return primes[num - 1] } -console.log(calculatePrime(num)); \ No newline at end of file +console.log(calculatePrime(num))