10 = (((1 + 5) + 2) + 2)
11 = ((1 + 5) + 5)
12 = (((1 + 5) + 3) + 3)
13 = (((1 + 5) + 5) + 2)
14 = (((1 + 5) + 5) + 3)
15 = ((((1 + 5) + 5) + 2) + 2)
16 = (((1 + 5) + 5) + 5)
17 = ((((1 + 5) + 5) + 3) + 3)
18 = ((((1 + 5) + 5) + 5) + 2)
19 = ((((1 + 5) + 5) + 5) + 3)
function findDecompositionSomme(target) {
function find(start, history) {
if (start == target)
return history;
else if (start > target)
return null;
else
return find(start + 5, `(${history} + 5 )`) ||
find(start + 3, `(${history} + 3 )`) ||
find(start + 2, `(${history} + 2 )`);
}
return `${target} = ${find(1 , "1")}`
}
console.log(findDecompositionSomme(10));
function find(start, history) {
if (start == target)
return history;
else if (start > target)
return null;
else
return find(start + 5, `(${history} + 5 )`) ||
find(start + 3, `(${history} + 3 )`) ||
find(start + 2, `(${history} + 2 )`);
}
return `${target} = ${find(1 , "1")}`
}
console.log(findDecompositionSomme(10));
Vous pourrez modifier le code pour faire des décompositions somme produit !
10 = (((1 + 3) + 3) + 3)
11 = (((1 * 5) + 3) + 3)
12 = null
13 = ((((1 + 3) + 3) + 3) + 3)
14 = ((((1 * 5) + 3) + 3) + 3)
15 = null
16 = (((((1 + 3) + 3) + 3) + 3) + 3)
17 = (((((1 * 5) + 3) + 3) + 3) + 3)
18 = null
19 = ((((((1 + 3) + 3) + 3) + 3) + 3) + 3)