JSFiddle - React, Tailwind, and code Playground
by Josh Leverington
TypeScript
function calcPieces(x) {
if (x < 1) {
return -1
}
if (x < 3) {
return 1;
}
let pieces = calcPieces(x - 1);
if ((pieces % x) === 0) {
return pieces;
}
return pieces * x;
}
function lcm(...arr) {
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
const _lcm = (x, y) => (x * y) / gcd(x, y);
return [...arr].reduce((a, b) => _lcm(a, b));
};
let arr=[];
for (let i = -5; i <= 10; i++) {
let y=-1;
if (i>0){
arr.push(i);
y=lcm(...arr);
}
console.log(i, calcPieces(i),y);
}