Javascript enters two integers n and m, and takes the sum of n between 1m and m

enter two integers n and m, take a few numbers at random from the sequence 1p2 and m, and make their sum equal to m. All possible combinations are required to be listed.

fn (5pm 7) = >

[1, br 2, 4]
[2, 5, 5]
[3, 4, 4]

Sep.22,2021

function f(max, min, m, t, mid, result) {
    if (t > m) {
        return
    }
    if (t === m) {
        result.push(mid)
        return
    }
    for (let i = min; i <= max; iPP) {
        f(max, i + 1, m, t + i, [...mid, i], result)
    }
}

var result = []
var m = 5
var n = 10
f(Math.min(m, n), 1, m, 0, [], result)

console.log(result)
Menu