-1

For example let's say I have arrays a = [1, 2, 3, 4, 5, 6] and b = a And the output I expect would be:

1 + 1, 1 + 2, 1 + 3 ... 3 + 1, 3 + 2, 3 + 4 ... 6 + 3, 6 + 4, 6 + 5, 6 + 6

I would prefer to make this simple calculation in JS or Ruby, but I don't mind answer in any other language. Can anyone provide me any direction?

Kunok
  • 8,089
  • 8
  • 48
  • 89
  • 2
    Have you tried to solve this problem yourself? If so, can you add the code to your question that you have already tried? – jehna1 Mar 12 '16 at 21:50
  • What you're trying to do is called permutations. Good in depth answer can be found here: http://stackoverflow.com/questions/9960908/permutations-in-javascript – dYale Mar 12 '16 at 21:54
  • @dYale, since the OP wants all possible sums, combinations are sufficient, and even those may have duplicate sums. – Cary Swoveland Mar 13 '16 at 03:07

6 Answers6

2
a = [1, 2, 3, 4, 5, 6]
b = [1, 4, -1, 7, 9]

a.product(b).map { |a,b| a+b }.uniq
  #=> [2, 5, 0, 8, 10, 3, 6, 1, 9, 11, 4, 7, 12, 13, 14, 15] 

The steps:

c = a.product(b)
  #=> [[1, 1], [1, 4], [1, -1], [1, 7], [1, 9],
  #    [2, 1], [2, 4], [2, -1], [2, 7], [2, 9],
  #    [3, 1], [3, 4], [3, -1], [3, 7], [3, 9],
  #    [4, 1], [4, 4], [4, -1], [4, 7], [4, 9],
  #    [5, 1], [5, 4], [5, -1], [5, 7], [5, 9],
  #    [6, 1], [6, 4], [6, -1], [6, 7], [6, 9]] 
d = c.map { |a,b| a+b }
  #=> [2, 5, 0, 8, 10,
  #    3, 6, 1, 9, 11,
  #    4, 7, 2, 10, 12,
  #    5, 8, 3, 11, 13,
  #    6, 9, 4, 12, 14,
  #    7, 10, 5, 13, 15] 
d.uniq
  #=> [2, 5, 0, 8, 10, 3, 6, 1, 9, 11, 4, 7, 12, 13, 14, 15] 
Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
2

In Ruby:

a.product(b).map {|p| p.reduce(:+) }.uniq
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
1

You can use Array.prototype.forEach() for the iteration over the arrays.

The forEach() method executes a provided function once per array element.

The result is without repeat.

function xSums(array) {
    var r = [],
        o = {};
    array.forEach(function (a) {
        array.forEach(function (b) {
            if (!o[a + b]) {
                r.push(b + a);
                o[a + b] = true;
            }
        });
    });
    return r;
}

document.write('<pre>' + JSON.stringify(xSums([1, 2, 3, 4, 5]), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(xSums([3, 7, 42]), 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

I would do something like this:

a = [1, 2, 3, 4, 5, 6]
a.permutation(2).map { |x, y| x + y }
spickermann
  • 100,941
  • 9
  • 101
  • 131
1

In javascript, get unique sums using Set

var a = [1, 2, 3, 4, 5, 6];
var r = new Set();
a.forEach(x => a.forEach(y => r.add(x + y)))

document.write('<pre>' + Array.from(r) + '</pre>');
isvforall
  • 8,768
  • 6
  • 35
  • 50
0

Try looping through both arrays, adding the first to the second, and storing the results in a third, eg;

var one = [1, 2, 3];
var two = [4, 5, 6];

var three = [];

for(var x = 0; x < one.length; ×++){
  for(var y = 0; y < two.length; y++){
    three.push(one[x] + two[y]);
  }
}

This would result in three[0] = 1 + 4, three[1] = 1+ 5, three[2] =1+ 6, three[3] = 2 + 4, three[4] = 2+ 5 etc...

Lewis
  • 3,479
  • 25
  • 40