I have a bunch of arrays of numbers for example:
let a = [1,2,3];
let b = [4,5,7];
let c = [11,13,17]
I want to create a function that tells me what combination(s) of numbers in the arrays multiplied by numbers in a different array result to a certain number. Below is the code I have so far:
// to get which combination of numbers result to 165
a.forEach(x=>{
b.forEach(y=>{
c.forEach(z=>{
if(x*y*z==165){
console.log(x,y,z)
}
})
})
})
I want to create a function that can be fed an arrays of arrays of numbers such as: [[1,2],[2,5,],[7,11],[13,17]]
and give back the combination(s) of numbers that, when multiplied, can result to a certain number.
Here's a recursive function that takes an input number and array of arrays of numbers. It processes through the first array of numbers, finding any that are potential divisors, and if it does, recursively calling itself with the divided input and the balance of the array to see if there are any divisors from that pair of values. If so, they are appended to the current divisor to produce the result:
const findDivisors = (num, arrs) => {
let result = [];
if (arrs.length == 1) {
return arrs[0].filter(n => n == num);
}
arrs[0].forEach(n => {
if (num % n === 0) {
findDivisors(num / n, arrs.slice(1)).forEach(r => result.push([n].concat(r)));
}
});
return result;
}
let a = [1, 2, 3];
let b = [4, 5, 7];
let c = [11, 13, 17];
console.log(findDivisors(165, [a, b, c]));
console.log(findDivisors(136, [a, b, c]));