Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Generating all combinations of array using javascript

    Hello Jscript Developers

    How can I generate all possible combinations of an array using javascript.

    For Ex: 1234
    Combinations required:
    1234
    1324
    1432
    ....
    ....
    4213
    4321

    and also I want to generate combinations based on factorial .For 7! we have to loop 720 times .is it any problem running such loops from client side

    Thanks and regards
    Muralikrishna

  • #2
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    This page gives all permutations off n numbers.
    Its uses only the first function getPrm (the second is getNmb) to get the permutation with is factorial number (or this number with the permutation). See wikipedia Factorial_number_system for further explanations.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title></title>
    <style type="text/css">
    </style>
    </head>
    <body id="bdy">
    <p>Permutations	
    		<select onchange="setOrd(this.value)">		
    			<option value="0">Order</option>		
    			<option value="1">1</option>		
    			<option value="2">2</option>		
    			<option value="3">3</option>		
    			<option value="4">4</option>		
    			<option value="5">5</option>
    			<option value="6">6</option>		
    			<option value="7">7</option>		
    			<option value="8">8</option>
    		</select>
    	</p>
    		<div id="rsp"></div>
    	</div>
    <script type="text/javascript">
    // Permutations http://en.wikipedia.org/wiki/Factorial_number_system
    // _ord order, _sto='123..._ord', _tbo the array [0!,1! ... (_ord-1)!] of length order
     
    var _ord,_tbo=[],_sto;
    // To update this global variables
    function setOrd(o){var i,l,str='';
    	document.getElementById('rsp').innerHTML='';
    	if (!o) return;
    	_ord=o;_tbo.length=0;
    	_sto='';i=0;while (i<o) {_tbo[i]=0<i?_tbo[i-1]*i:1;_sto+=++i;}
    	// To edit all permutations
    	for (l=_tbo[i-1]*i,i=0;i<l;i++) str+='n°'+i+' : '+getPrm(i)+'<br>';
    	document.getElementById('rsp').innerHTML='<p>'+str+'</p>'; 
    }
    // n (from 0 to _ord!-1) => a permutation of sto
    function getPrm(n){var i=_ord,s=_sto,t=_tbo,p='',j;
    	 while(i--) {j=Math.floor(n/t[i]);n%=t[i];
    		p+=s.substr(j,1);s=s.substr(0,j)+s.substr(j+1);}
    	return p;
    }
    // a permutation of a string ('3412...') => its number (from 0 to _ord!-1)
    function getNmb(p){var i=_ord,j,s=[],t=_tbo,n=0;
    	while (i--) {j=p.substr(i,1);s[s.length]=j;s.sort();
    		n+=s.join('').indexOf(j)*t[i];}
    	return n;
    } 
    </script>
    </body>
    </html>
    To find the 15 th permutation of 1234 (from 0 to 23)
    1/ - divide 15 by 3!=6 witch is 2 => to take the number of rank 2 of '1234' witch is '3'
    2/ - divide the rest by 2!=2 witch is 1 => to take the number of rank 1 of '124' witch is '2' =>'32'
    3/ - divide the rest by 1!=1 witch is 1 => to take take the number of rank 1 of '14' witch is '4' =>'324'
    4/ - divide the rest by 0!=1 witch is 0 => to take take the number of rank 0 of '1' witch is '1' =>'3241'

    With this functions there is no need to calculate all permutation to find one (or inversely)...
    Last edited by 007julien; 08-16-2013 at 06:47 PM.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Here you are:-


    Code:
    <script type = "text/javascript">
    
    var keywords1 = [1,2,3,4]; 
    var keywords2 = [1,2,3,4]; 
    var keywords3 = [1,2,3,4]; 
    var keywords4 = [1,2,3,4];
    var combs = [];
    var count = 0;      
    
    var z1 = keywords1.length; 
    var z2 = keywords2.length; 
    var z3 = keywords3.length; 
    var z4 = keywords4.length;
    
    for (var h = 0; h<z4; h++) {
    for (var i = 0; i<z3; i++) {
    for (var j = 0; j<z2; j++) {
    for (var k = 0; k<z1; k++) {
    combs[count] = keywords1[k] + " " + keywords2[j] + " " + keywords3[i] + " " + keywords4[h];
    count++;
    }
    }
    }
    }
    
    for (var i =0; i <count; i++) {
    document.write(combs[i] + "<br>")
    }
    
    </script>
    720 loops ought not to be a problem.
    7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5,040 6! is 720.
    But do you mean combinations or permutations?


    Quizmaster: What animals do you associate with gymkhanas?
    Contestant: Elephants
    Last edited by Philip M; 08-16-2013 at 05:53 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •