Files
ValueScript/inputs/passing/projEuler/p28.ts
Andrew Morris c65f196121 p28
2023-03-10 11:00:37 +11:00

38 lines
831 B
TypeScript

import plus from "./helpers/plus.ts";
export default function () {
return [
1,
raySum([1, 9, 25], 501) - 1,
raySum([1, 3, 13], 501) - 1,
raySum([1, 5, 17], 501) - 1,
raySum([1, 7, 21], 501) - 1,
].reduce(plus);
}
type QuadraticTriplet = [number, number, number];
/**
* There's an analytic solution for this, but that kinda eliminates the need to
* write code altogether. We're showcasing programming techniques here, not
* just writing down the simplest solution.
*/
function raySum(triplet: QuadraticTriplet, len: number) {
let sum = triplet.reduce(plus);
for (let i = 3; i < len; i++) {
triplet = nextTriplet(triplet);
sum += triplet[2];
}
return sum;
}
function nextTriplet([a, b, c]: QuadraticTriplet): QuadraticTriplet {
return [
b,
c,
3 * c - 3 * b + a,
];
}