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.
This commit is contained in:
committed by
mrugesh
parent
f5feff386a
commit
e4a229187e
@ -62,44 +62,67 @@ divisibleTriangleNumber(500);
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='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;
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -35,8 +35,8 @@ tests:
|
||||
testString: assert.strictEqual(longestCollatzSequence(46500), 35655);
|
||||
- text: <code>longestCollatzSequence(54512)</code> should return 52527.
|
||||
testString: assert.strictEqual(longestCollatzSequence(54512), 52527);
|
||||
- text: <code>longestCollatzSequence(1000000)</code> should return 837799.
|
||||
testString: assert.strictEqual(longestCollatzSequence(1000000), 837799);
|
||||
- text: <code>longestCollatzSequence(100000)</code> should return 77031.
|
||||
testString: assert.strictEqual(longestCollatzSequence(100000), 77031);
|
||||
|
||||
```
|
||||
|
||||
|
@ -61,21 +61,22 @@ nthPrime(10001);
|
||||
|
||||
|
||||
```js
|
||||
const nthPrime = (number)=>{
|
||||
let pN = 2;
|
||||
let step = 0;
|
||||
while (step<number) {
|
||||
let isPrime = true;
|
||||
for(let i = 2;i<pN;i++){
|
||||
if(!(pN%i)){
|
||||
const nthPrime = n => {
|
||||
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;
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user