(euler): #12
This commit is contained in:
parent
4d86c70168
commit
df52533913
2 changed files with 41 additions and 1 deletions
39
euler/src/12 - Highly Divisible Triangular Number.ts
Normal file
39
euler/src/12 - Highly Divisible Triangular Number.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
export {};
|
||||
|
||||
const factorsOf = (num: number) => {
|
||||
const isEven = num % 2 === 0;
|
||||
const max = Math.sqrt(num);
|
||||
const inc = isEven ? 1 : 2;
|
||||
const factors = [1, num];
|
||||
|
||||
for (let curFactor = isEven ? 2 : 3; curFactor <= max; curFactor += inc) {
|
||||
if (num % curFactor !== 0) continue;
|
||||
factors.push(curFactor);
|
||||
|
||||
let compliment = num / curFactor;
|
||||
if (compliment !== curFactor) factors.push(compliment);
|
||||
}
|
||||
|
||||
return factors;
|
||||
};
|
||||
|
||||
// https://www.mathsisfun.com/algebra/triangular-numbers.html
|
||||
const nthTriangleNumber = (n: number) => (n * (n + 1)) / 2;
|
||||
|
||||
const firstTriangleWithOverNDivisors = (n: number) => {
|
||||
let divisorCountFound = false;
|
||||
let i = 1;
|
||||
|
||||
while (!divisorCountFound) {
|
||||
const triangle = nthTriangleNumber(i);
|
||||
const factors = [...factorsOf(triangle)];
|
||||
i++;
|
||||
|
||||
if (factors.length > n) {
|
||||
divisorCountFound = true;
|
||||
return triangle;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
console.log(firstTriangleWithOverNDivisors(500));
|
|
@ -2,7 +2,8 @@
|
|||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"module": "commonjs",
|
||||
"outDir": "build"
|
||||
"outDir": "build",
|
||||
"downlevelIteration": true
|
||||
},
|
||||
"include": ["src/**/*.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
|
|
Loading…
Reference in a new issue