JSFiddle - React, Tailwind, and code Playground
HTML
<div id="results"></div>
CSS
body { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; }
p { margin: 1px; }
JavaScript
// the function, no recursive
function createVector(size) {
// start with 0-size vector, so one empty array
let permutations = [[]];
let newPermutations = [];
// create permutations of given size
for (let i = 0; i < size; i++) {
// 0 to 10 (will be mapped to 0.0 to 1.0)
for (let j = 0; j <= 10; j++) {
// iterate over each current permutation
outer:
for (let k = permutations.length - 1; k >= 0; k--) {
const permutation = permutations[k];
// check if we can add a value
let sum = j;
for (let l = permutation.length - 1; l >= 0; l--) {
sum += permutation[l];
if (sum > 10) {
// vector sum exceeds maximum
continue outer;
}
}
// add this new vector
newPermutations.push([j].concat(permutation));
}
}
// permutations contains the current set of vector
permutations = newPermutations;
// newPermutations will contain the next set
newPermutations = [];
}
// filter invalid vectors which sum is not 10 (then mapped to 1.0)
for (let i = 0; i < permutations.length; i++) {
const permutation = permutations[i];
let sum = 0;
for (let j = 0; j < permutation.length; j++) {
// calc sum
sum += permutation[j];
// and map 0 - 10 to 0.0 - 1.0
permutation[j] /= 10;
}
if (sum === 10) {
newPermutations.push(permutation);
}
}
return newPermutations;
}
// test code
const vectors = createVector(4);
const div = document.querySelector('#results');
for (let i = 0; i < vectors.length; i++) {
const p = document.createElement('p');
p.textContent = JSON.stringify(vectors[i]).replace(/,/g, ', ');
div.appendChild(p);
}