SmartyPants1231
New member
- Joined
- Oct 22, 2020
- Messages
- 2
How many ways can you make 20 by using 1,5,7,8, and 10?
				
			One easy way I can think of:How many ways can you make 20 by using 1,5,7,8, and 10?
Also, what counts as "different ways"? Is 5+7+8 different from 7+5+8? or from 5+(7+8)?How many ways can you make 20 by using 1,5,7,8, and 10?
// Available numbers
let numbers = [ 1, 5, 7, 8, 10 ];
// Process all distinct combinations of numbers
for (let set = [ 0 ]; set.length < 21;) {
    // Calculate the total sum of the current combination
    let total = 0;
    for (let x of set)
        total += numbers[x];
    // The current combination sums to 20
    if (total == 20) {
        let output = [];
        for (let x of set)
            output.push(numbers[x]);
        console.log(output);
    }
    // Advance to the next combination
    for (let x = 0; x < set.length; x++) {
        // Select the next number
        set[x]++;
        // The number is in the list of available numbers
        if (set[x] < numbers.length) {
            // Set all previous elements to the current number
            for (let y = x - 1; y >= 0; y--)
                set[y] = set[x];
            break;
        }
        // All elements in the list are the last available number
        if (x == set.length - 1)
            set.push(-1); // Add a new element to the list
    }
}