From e4a229187ef035bffeff1175c23c9be246fb0761 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Thu, 12 Dec 2019 13:02:58 +0100 Subject: [PATCH] fix: speed up tests Either improved the algorithm or reduced the difficulty for the slowest tests. All these pass with testProtectTimeout = 500, so should be safe on most machines with the current timeout of 2500. --- ...hly-divisible-triangular-number.english.md | 65 +++++++++++++------ ...lem-14-longest-collatz-sequence.english.md | 4 +- .../problem-7-10001st-prime.english.md | 23 +++---- .../rosetta-code/emirp-primes.english.md | 4 +- 4 files changed, 60 insertions(+), 36 deletions(-) diff --git a/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-12-highly-divisible-triangular-number.english.md b/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-12-highly-divisible-triangular-number.english.md index e2a97c4a89..ec923be79e 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-12-highly-divisible-triangular-number.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-12-highly-divisible-triangular-number.english.md @@ -62,44 +62,67 @@ divisibleTriangleNumber(500); - - ## Solution
- ```js function divisibleTriangleNumber(n) { + if (n === 1) return 3; let counter = 1; let triangleNumber = counter++; - function getFactors(num) { - let factors = []; - let possibleFactor = 1; - let sqrt = Math.sqrt(num); + while (noOfFactors(triangleNumber) < n) { + triangleNumber += counter++; + } +return triangleNumber; +} - while (possibleFactor <= sqrt) { - if (num % possibleFactor == 0) { - factors.push(possibleFactor); - var otherPossibleFactor = num / possibleFactor; - if (otherPossibleFactor > possibleFactor) { - factors.push(otherPossibleFactor); - } +function noOfFactors(num) { + const primeFactors = getPrimeFactors(num); + let prod = 1; + for(let p in primeFactors) { + prod *= (primeFactors[p] + 1) + } + return prod; +} + +function getPrimeFactors(num) { + let n = num; + let primes = {}; + + let p = 2; + let sqrt = Math.sqrt(num); + + function checkAndUpdate(inc) { + if (n % p === 0) { + const curr = primes[p]; + if (curr) { + primes[p]++ + } else { + primes[p] = 1; } - possibleFactor++; + n /= p; + } else { + p += inc; } - - return factors; } - while (getFactors(triangleNumber).length < n) { - triangleNumber += counter++; + while(p === 2 && p <= n) { + checkAndUpdate(1); } - console.log(triangleNumber) - return triangleNumber; + + while (p <= n && p <= sqrt) { + checkAndUpdate(2); + } + if(Object.keys(primes).length === 0) { + primes[num] = 1; + } else if(n !== 1) { + primes[n] = 1; + } + return primes; } ``` diff --git a/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-14-longest-collatz-sequence.english.md b/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-14-longest-collatz-sequence.english.md index a550092b37..3d6ad37cbe 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-14-longest-collatz-sequence.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-14-longest-collatz-sequence.english.md @@ -35,8 +35,8 @@ tests: testString: assert.strictEqual(longestCollatzSequence(46500), 35655); - text: longestCollatzSequence(54512) should return 52527. testString: assert.strictEqual(longestCollatzSequence(54512), 52527); - - text: longestCollatzSequence(1000000) should return 837799. - testString: assert.strictEqual(longestCollatzSequence(1000000), 837799); + - text: longestCollatzSequence(100000) should return 77031. + testString: assert.strictEqual(longestCollatzSequence(100000), 77031); ``` diff --git a/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-7-10001st-prime.english.md b/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-7-10001st-prime.english.md index ab7834db33..436dfc60b0 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-7-10001st-prime.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-7-10001st-prime.english.md @@ -61,21 +61,22 @@ nthPrime(10001); ```js -const nthPrime = (number)=>{ - let pN = 2; - let step = 0; - while (step { + let pN = 2; + let step = 0; + while (step < n) { + let isPrime = true; + let rootN = Math.sqrt(pN); + for (let i = 2; i <= rootN; i++) { + if (!(pN % i)) { isPrime = false; break; } - } - isPrime ? step++ : ''; + } + isPrime ? step++ : ''; pN++; - } - return pN-1; + } + return pN - 1; } ``` diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/emirp-primes.english.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/emirp-primes.english.md index 6dd20798cb..0970348726 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/emirp-primes.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/emirp-primes.english.md @@ -31,8 +31,8 @@ tests: testString: assert(typeof emirps === 'function'); - text: emirps(20,true) should return [13,17,31,37,71,73,79,97,107,113,149,157,167,179,199,311,337,347,359,389] testString: assert.deepEqual(emirps(20, true), [13, 17, 31, 37, 71, 73, 79, 97, 107, 113, 149, 157, 167, 179, 199, 311, 337, 347, 359, 389]); - - text: emirps(10000) should return 948349 - testString: assert.deepEqual(emirps(10000), 948349); + - text: emirps(1000) should return 70529 + testString: assert.deepEqual(emirps(1000), 70529); - text: emirps([7700,8000],true) should return [7717,7757,7817,7841,7867,7879,7901,7927,7949,7951,7963] testString: assert.deepEqual(emirps([7700, 8000], true), [7717, 7757, 7817, 7841, 7867, 7879, 7901, 7927, 7949, 7951, 7963]); - text: emirps([7700,8000],true) should return 11