EDIT:
var Combos = [1, 2, 4],
Target = 4;
for(var i = 0; i < Combos.length; i++){
var Available = [];
for(var j = -1; j < i; j++) Available.push(Combos[j+1]);
for(var k = 0; k < Available.length; k++){
var C = 0;
var Att = 0;
while(C < Target){ C += Available[k]; Att++; }
if(C === Target) console.log(new Int8Array(Att).fill(Available[k]));
}
}
This outputs:
[1, 1, 1, 1]
[1, 1, 1, 1]
[2, 2]
[1, 1, 1, 1]
[2, 2]
[4]
I don't know why I'm getting the repetition of 1, 1, 1, 1 and 2, 2 but I'm investigating!
My provided numbers array will be [1, 2, 4], I'm trying to get to a target that will always fit; let's assume this is 4.
How can I return an array of combinations where the process reuses the provided numbers, for example: all of the questions I've looked at on here only use the numbers once, and thus these programs would return [[4]]
, whereas I'd like the result to be [[1, 1, 1, 1], [1, 1, 2], [2, 2], [4]]
.
At first, I thought about using modulus to say: is the target an integer, if so we can instantly return target / 1. But I honestly can't figure out the logic!
An IRC chat I've had, perhaps this might help you understand:
13:20 Robinlemon So I have the numbers [1, 2] and I'm trying to make 4 so I want a function that will return [[1, 1, 1, 1], [1, 1 ,2], [2, 2], [4]] -> all the possible combinations to make the target, by reusing the numbers supplied
13:21 kakashiAL how does your function api looks like?
13:21 Robinlemon What do you mean?
13:21 Robinlemon I've scrapped everything
13:21 kakashiAL foo(myArray, combination=
13:21 Robinlemon Because I can't ever work out the logic to do this
13:21 Robinlemon Well I suppose i'd have
13:22 Robinlemon Permutate(Price) => SubPermutate()
13:22 Robinlemon Then return
13:22 Robinlemon and tierate through each sub function
13:22 kakashiAL so you have [1, 2] and you want to make 4, which means you want to have this:
13:22 kakashiAL 1, 1, 1, 1
13:22 kakashiAL 2, 2, 2, 2
13:23 kakashiAL 1, 2, 2, 2
13:23 kakashiAL 1, 1, 2, 2
13:23 kakashiAL and so on
13:23 kakashiAL right?
13:26 Robinlemon yep
13:26 Robinlemon well no
13:26 Robinlemon The numbers need to add to 4
13:26 Robinlemon so it wouldnt be 1, 1, 2, 2
13:26 Robinlemon it would be 1 1 2
13:26 Robinlemon get it
13:26 kakashiAL you have the numbers 1, 2 and 4, correct?
13:28 Robinlemon yes
13:28 Robinlemon For example sake
13:29 Robinlemon It should work with anything tho
13:29 Robinlemon The target will always be reachable with the numbers
13:29 kakashiAL okay, now you want to get this, with 1, 2 and 4:
13:29 kakashiAL 1, 1, 1
13:29 kakashiAL 2, 2, 2
13:29 kakashiAL 4, 4, 4
13:29 kakashiAL 1, 2, 4
13:29 kakashiAL 1, 1, 2
13:29 Robinlemon No
13:29 Robinlemon 1, 1, 1, 1
13:29 Robinlemon 2, 2
13:29 Robinlemon 4
13:30 Robinlemon The numbers need to add to 4
13:30 Robinlemon I wnat the combinations that add to 4
13:30 kakashiAL ahh if you have 6 you would have this:
13:30 kakashiAL 1, 1, 1, 1, 1, 1
13:30 kakashiAL 2, 2, 2
13:30 Robinlemon Yes
Thanks for reading, I look forward to looking at your replies!