It is necessary to optimize the function

I am working on this function, which should return all possible values โ€‹โ€‹of adding a and b n times, for example, if n = 1 , then the possible values โ€‹โ€‹will be a + a a + b and b + b . the function below works, but it is too slow, and I want to optimize it. Any suggestions? Thank you very much!

 function processData(n, a, b){ var ans = [0]; for(var i = 0; i < n; i++){ var temp = []; for(var j = 0; j < ans.length; j++){ var aa = ans[j] + a; if(temp.includes(aa) === false){ temp.push(aa); } var bb = ans[j] + b; if(temp.includes(bb) === false){ temp.push(bb); } } ans = temp; } ans.sort(function(a, b){return a - b}); return ans; } 
+5
source share
5 answers
 function processData(n, a, b) { var ans = []; if (a == b) { for (var i=0; i<n+1; i++) { ans.push(a * n); } return ans; } else if (a > b) { var temp = a; a = b; b = temp; } var diff = b - a; for (var i=0; i<n+1; i++) { ans.push(a * n + diff * i); } return ans; } 

Well, this is by far the most effective solution. I just tested it on fiddle .

All the other three solutions are significantly superior to yours. Mine is better than @ abc123 because there is no need for sorting and better than @georg because there is no need to use a set or sort.

+4
source

This is an offer that gets posts for a and b.

 function combine(left, right) { function carry() { return c.reduceRight(function (r, _, i, o) { return r && !(o[i] = (o[i] + 1) % left.length); }, 1); } var c = Array.apply(null, { length: right.length }).map(function () { return 0; }), result = []; do { result.push(c.reduce(function (r, a, i) { r[left[a]].push(right[i]); return r; }, left.reduce(function (r, a) { r[a] = []; return r; }, {}))); } while (!carry()); return result; } console.log(combine(['a', 'b'], [1, 2, 3])); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Just to make a disclosure, the answer is one of mine, asked a more detailed question about the same, but not quite: Find all combinations of two arrays

+2
source

Here is a brief ES6 function for it:

 function processData(n, a, b) { let diff = Math.abs(ba), sum = Math.min(a,b) * (n+1) - diff; return diff ? Array.from(Array(n+2), _ => sum += diff) : [sum]; } console.log(processData(2, 3, 5)); 
+2
source

Maybe it's faster

 function process(a,b,n){ out=[]; //get difference f=ab; var last=b*n; for(i=0;i<n;i++){ out.push(last); last+=f; } return out; } 

I am not sure if this is correct, as checked with only a few values. I think you want more.

+1
source

Simplified algorithm

 // a+a 2 a 0 b // a+b, b+a 1a 1b // b+b 0a 2b // a+a+a, 3a 0b // a+b+a, a+a+b 2a 1b // b+b+a, a+b+b 1a 2b // b+b+b 0a 3b // a+a+a+a 4a 0b // a+a+a+b, b+a+a+a, a+a+b+a, a+b+a+a 3a 1b // a+a+b+b, a+b+a+b, a+b+b+a, b+a+a+b, b+a+b+a, b+b+a+a 2a 2b // a+b+b+b, b+a+b+b, b+b+a+b, b+b+b+a 1a 3b // b+b+b+b 4b // a+a+a+a+a 5a 0b // a+a+a+a+b, a+a+a+b+a, a+a+b+a+a, a+b+a+a+a, b+a+a+a+a+a 4a 1b // a+a+a+b+b, a+a+b+a+b, a+a+b+b+a, a+b+a+a+b, a+b+a+b+a, a+b+b+a+a, b+a+a+a+b, b+a+a+b+a, b+a+b+a+a, b+b+a+a+a 3a 2b // a+a+b+b+b, a+b+a+b+b 2a 3b // a+b+b+b+b 1a 4b // b+b+b+b+b 0a 5b function processData(n, a, b){ var ans = []; for(var i = 0; i < n + 1; i++){ aa = a * (ni); bb = b * (i); ans.push(aa + bb) } ans.sort(function(a, b){return a - b}); return ans; } console.log(processData(3, 1, 2)); 
+1
source

Source: https://habr.com/ru/post/1259979/


All Articles