An algorithm that finds a sequence of numbers to fill a square grid
While not a complete solution for your first example, you can do two things to help speed it up.

If you don't have to use an array of values, don't just use a min and max (though the code doesn't change much if you do use a range).

You also don't need the third for-loop. Your third number will always be target - x - y. If target isn't in the range, you can throw it away.

You can further limit your for loop because you don't need to go all the way through every number in both loops. Since you know that you can't have 2 numbers which are both greater than half of your target, you can go through just the first part in the first loop, and the second loop through the second part. This lets you trim off a good bit from your for loops.

I also ended up using it as a string for this part, since it makes the check for uniqueness a bit easier, though it's not strictly necessary. My reduce also ensures no double digits.



const min = 1;
const max = 8;
const target = 12;

const allCombinations = (target, min, max) =&gt; {
  const combos = [];
  for (let i = min; i &lt;= target / 2 &amp;&amp; i &lt;= max; i++) {
    for (let j = max; j &gt;= target / 2 &amp;&amp; i &gt;= min &amp;&amp; target - i - j &gt;= min; j--) {
      combos.push([i, j, target - i - j].sort().join(''));
    }
  }
  
  return combos
    .reduce((result, a) =&gt; result.concat(!result.includes(a) &amp;&amp; a[0] !== a[1] &amp;&amp; a[1] !== a[2] &amp;&amp; a[2] !== a[0] ? a : []), []);
};

console.log(allCombinations(target, min, max));