2

I want to generate an array of all the permutations for a time-series. Suppose the numbers can be 0, 5, 10, 25 and the first permutation is [0,0,0,0,0,0,0]. The next permutation can be [0,0,0,0,0,0,5] and so on up until [25,25,25,25,25,25,25]. There should be 4^6 = 4096 permutations in this case because there are 4 numbers and 7 slots.

Can someone please help me understand how to get started on this problem? I want to write this in javascript. Thanks for your consideration.

AS3Noob
  • 131
  • 5
  • Would an array make more sense, or just a readily-accessed function, that could be called by similar index? You'd probably use a fair amount of memory with the array. – Katana314 Feb 06 '15 at 14:20
  • I'd use a for with some conditions, e.g. `for(i = 0;i < 4096;i + 5)` and use some functions to manage the amount carried (when you reach 25 you set that one to 0 and the next to the left to 5) – Phate01 Feb 06 '15 at 14:23
  • Those are not [permutations](https://en.wikipedia.org/wiki/Permutation). You seem to want all strings of size 7 over the alphabet `{0, 5, 10, 25}`. – Bergi Feb 06 '15 at 14:44

2 Answers2

1

See the attached script, something i just put together. It should work in your case. I've limited to 4 permutations, but it should be easy to expand to 7. I hope you see the pattern.

var array = new Array();
var values = [0,5,10,25];

for(var i = 0; i < Math.pow(4,4); i++) {
  
  // calculate which indexes to retrieve value from loops through 1..4
  var entry = [
    Math.floor(i / Math.pow(4,0)) % 4, // increment this with every i
    Math.floor(i / Math.pow(4,1)) % 4, // increment this with every 4 * i
    Math.floor(i / Math.pow(4,2)) % 4, // increment this with every 16 * i
    Math.floor(i / Math.pow(4,3)) % 4  // increment this with every 64 * i etc
  ];
  
  array.push([values[entry[0]], values[entry[1]], values[entry[2]],values[entry[3]]]);
   
}

document.write(JSON.stringify(array));
jonas
  • 984
  • 10
  • 15
0

Below, I created a loop to generate all permutations. It is an iterative solution.

Update: I have reduced my code down to the following:

function permutations(series, size) {
  return zeroFill(Math.pow(series.length,size)).map(function(r,i) {
    return zeroFill(size).map(function(c,j) {
      return series[Math.floor(i/Math.pow(series.length,j))%series.length];
    }).reverse();
  });
}
function zeroFill(n) {
  return new Array(n+1).join('0').split('');
}

Since this is a very memory intensive process, I remove 25 from the list and reduced each processed entry to a length of 5.

var out = document.getElementById('output');
var permutations = generate([0, 5, 10], 5);

printResults(permutations);

// Generate permutations.
function generate(series, size) {
  var result = [];
  var len = series.length;
  var limit = Math.pow(len, size);

  for (var i = 0; i < limit; i++) {
    var entry = [];
    for (var j = 0; j < size; j++) {
      entry[size-j] = series[Math.floor(i / Math.pow(len, j)) % len];
    }
    result.push(entry);
  }
  return result;
}

// Utility Functions - These are for display purposes.
function printResults(list) {
  var width = ('' + list.length).length;
  list.forEach(function(list, index) {
    print('[' + FormatNumberLength(index, width) + '] ' + numberJoin(list, 3));
  });
}
function print(text) {
  out.innerHTML += text + '<br />';
}
function numberJoin(intList, width) {
  return intList.map(function(value) {
    return FormatNumberLength(value, width);
  }).join(' ');
}
function FormatNumberLength(num, length, useZero) {
  var c = useZero ? '0' : ' ';
  var r = "" + num;
  while (r.length < length) r = c + r;
  return r;
}
#output {
  font-family: monospace;
  white-space: pre;
}
<div id="output"></div>

Expected result

[  0]    0   0   0   0   0
[  1]    0   0   0   0   5
[  2]    0   0   0   0  10
[  3]    0   0   0   5   0
[  4]    0   0   0   5   5
[  5]    0   0   0   5  10
[  6]    0   0   0  10   0
[  7]    0   0   0  10   5
[  8]    0   0   0  10  10
[  9]    0   0   5   0   0
[ 10]    0   0   5   0   5
[ 11]    0   0   5   0  10
[ 12]    0   0   5   5   0
[ 13]    0   0   5   5   5
[ 14]    0   0   5   5  10
[ 15]    0   0   5  10   0
[ 16]    0   0   5  10   5
[ 17]    0   0   5  10  10
[ 18]    0   0  10   0   0
[ 19]    0   0  10   0   5
[ 20]    0   0  10   0  10
[ 21]    0   0  10   5   0
[ 22]    0   0  10   5   5
[ 23]    0   0  10   5  10
[ 24]    0   0  10  10   0
[ 25]    0   0  10  10   5
[ 26]    0   0  10  10  10
[ 27]    0   5   0   0   0
[ 28]    0   5   0   0   5
[ 29]    0   5   0   0  10
[ 30]    0   5   0   5   0
[ 31]    0   5   0   5   5
[ 32]    0   5   0   5  10
[ 33]    0   5   0  10   0
[ 34]    0   5   0  10   5
[ 35]    0   5   0  10  10
[ 36]    0   5   5   0   0
[ 37]    0   5   5   0   5
[ 38]    0   5   5   0  10
[ 39]    0   5   5   5   0
[ 40]    0   5   5   5   5
[ 41]    0   5   5   5  10
[ 42]    0   5   5  10   0
[ 43]    0   5   5  10   5
[ 44]    0   5   5  10  10
[ 45]    0   5  10   0   0
[ 46]    0   5  10   0   5
[ 47]    0   5  10   0  10
[ 48]    0   5  10   5   0
[ 49]    0   5  10   5   5
[ 50]    0   5  10   5  10
[ 51]    0   5  10  10   0
[ 52]    0   5  10  10   5
[ 53]    0   5  10  10  10
[ 54]    0  10   0   0   0
[ 55]    0  10   0   0   5
[ 56]    0  10   0   0  10
[ 57]    0  10   0   5   0
[ 58]    0  10   0   5   5
[ 59]    0  10   0   5  10
[ 60]    0  10   0  10   0
[ 61]    0  10   0  10   5
[ 62]    0  10   0  10  10
[ 63]    0  10   5   0   0
[ 64]    0  10   5   0   5
[ 65]    0  10   5   0  10
[ 66]    0  10   5   5   0
[ 67]    0  10   5   5   5
[ 68]    0  10   5   5  10
[ 69]    0  10   5  10   0
[ 70]    0  10   5  10   5
[ 71]    0  10   5  10  10
[ 72]    0  10  10   0   0
[ 73]    0  10  10   0   5
[ 74]    0  10  10   0  10
[ 75]    0  10  10   5   0
[ 76]    0  10  10   5   5
[ 77]    0  10  10   5  10
[ 78]    0  10  10  10   0
[ 79]    0  10  10  10   5
[ 80]    0  10  10  10  10
[ 81]    5   0   0   0   0
[ 82]    5   0   0   0   5
[ 83]    5   0   0   0  10
[ 84]    5   0   0   5   0
[ 85]    5   0   0   5   5
[ 86]    5   0   0   5  10
[ 87]    5   0   0  10   0
[ 88]    5   0   0  10   5
[ 89]    5   0   0  10  10
[ 90]    5   0   5   0   0
[ 91]    5   0   5   0   5
[ 92]    5   0   5   0  10
[ 93]    5   0   5   5   0
[ 94]    5   0   5   5   5
[ 95]    5   0   5   5  10
[ 96]    5   0   5  10   0
[ 97]    5   0   5  10   5
[ 98]    5   0   5  10  10
[ 99]    5   0  10   0   0
[100]    5   0  10   0   5
[101]    5   0  10   0  10
[102]    5   0  10   5   0
[103]    5   0  10   5   5
[104]    5   0  10   5  10
[105]    5   0  10  10   0
[106]    5   0  10  10   5
[107]    5   0  10  10  10
[108]    5   5   0   0   0
[109]    5   5   0   0   5
[110]    5   5   0   0  10
[111]    5   5   0   5   0
[112]    5   5   0   5   5
[113]    5   5   0   5  10
[114]    5   5   0  10   0
[115]    5   5   0  10   5
[116]    5   5   0  10  10
[117]    5   5   5   0   0
[118]    5   5   5   0   5
[119]    5   5   5   0  10
[120]    5   5   5   5   0
[121]    5   5   5   5   5
[122]    5   5   5   5  10
[123]    5   5   5  10   0
[124]    5   5   5  10   5
[125]    5   5   5  10  10
[126]    5   5  10   0   0
[127]    5   5  10   0   5
[128]    5   5  10   0  10
[129]    5   5  10   5   0
[130]    5   5  10   5   5
[131]    5   5  10   5  10
[132]    5   5  10  10   0
[133]    5   5  10  10   5
[134]    5   5  10  10  10
[135]    5  10   0   0   0
[136]    5  10   0   0   5
[137]    5  10   0   0  10
[138]    5  10   0   5   0
[139]    5  10   0   5   5
[140]    5  10   0   5  10
[141]    5  10   0  10   0
[142]    5  10   0  10   5
[143]    5  10   0  10  10
[144]    5  10   5   0   0
[145]    5  10   5   0   5
[146]    5  10   5   0  10
[147]    5  10   5   5   0
[148]    5  10   5   5   5
[149]    5  10   5   5  10
[150]    5  10   5  10   0
[151]    5  10   5  10   5
[152]    5  10   5  10  10
[153]    5  10  10   0   0
[154]    5  10  10   0   5
[155]    5  10  10   0  10
[156]    5  10  10   5   0
[157]    5  10  10   5   5
[158]    5  10  10   5  10
[159]    5  10  10  10   0
[160]    5  10  10  10   5
[161]    5  10  10  10  10
[162]   10   0   0   0   0
[163]   10   0   0   0   5
[164]   10   0   0   0  10
[165]   10   0   0   5   0
[166]   10   0   0   5   5
[167]   10   0   0   5  10
[168]   10   0   0  10   0
[169]   10   0   0  10   5
[170]   10   0   0  10  10
[171]   10   0   5   0   0
[172]   10   0   5   0   5
[173]   10   0   5   0  10
[174]   10   0   5   5   0
[175]   10   0   5   5   5
[176]   10   0   5   5  10
[177]   10   0   5  10   0
[178]   10   0   5  10   5
[179]   10   0   5  10  10
[180]   10   0  10   0   0
[181]   10   0  10   0   5
[182]   10   0  10   0  10
[183]   10   0  10   5   0
[184]   10   0  10   5   5
[185]   10   0  10   5  10
[186]   10   0  10  10   0
[187]   10   0  10  10   5
[188]   10   0  10  10  10
[189]   10   5   0   0   0
[190]   10   5   0   0   5
[191]   10   5   0   0  10
[192]   10   5   0   5   0
[193]   10   5   0   5   5
[194]   10   5   0   5  10
[195]   10   5   0  10   0
[196]   10   5   0  10   5
[197]   10   5   0  10  10
[198]   10   5   5   0   0
[199]   10   5   5   0   5
[200]   10   5   5   0  10
[201]   10   5   5   5   0
[202]   10   5   5   5   5
[203]   10   5   5   5  10
[204]   10   5   5  10   0
[205]   10   5   5  10   5
[206]   10   5   5  10  10
[207]   10   5  10   0   0
[208]   10   5  10   0   5
[209]   10   5  10   0  10
[210]   10   5  10   5   0
[211]   10   5  10   5   5
[212]   10   5  10   5  10
[213]   10   5  10  10   0
[214]   10   5  10  10   5
[215]   10   5  10  10  10
[216]   10  10   0   0   0
[217]   10  10   0   0   5
[218]   10  10   0   0  10
[219]   10  10   0   5   0
[220]   10  10   0   5   5
[221]   10  10   0   5  10
[222]   10  10   0  10   0
[223]   10  10   0  10   5
[224]   10  10   0  10  10
[225]   10  10   5   0   0
[226]   10  10   5   0   5
[227]   10  10   5   0  10
[228]   10  10   5   5   0
[229]   10  10   5   5   5
[230]   10  10   5   5  10
[231]   10  10   5  10   0
[232]   10  10   5  10   5
[233]   10  10   5  10  10
[234]   10  10  10   0   0
[235]   10  10  10   0   5
[236]   10  10  10   0  10
[237]   10  10  10   5   0
[238]   10  10  10   5   5
[239]   10  10  10   5  10
[240]   10  10  10  10   0
[241]   10  10  10  10   5
[242]   10  10  10  10  10
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132