Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
use strict;
use warnings;
my@symbols=('a','b','c','d');
my$start=join'',map{chr}(0..$#symbols);
my$refallperms=permutations(\$start);
for my$p(@{$refallperms}){
for my$q(0..$#symbols){
print$symbols[ord(substr($p,$q))],' ';
}
print"\n";
}
print'Number of permutations of ',$#symbols+1," symbols: ",scalar@{$refallperms},"\n";
#####################################################
sub permutations{
my($refin)=@_;
#reference to a string containing n different chr's
return[$$refin]if length($$refin)==1;
local($_);
my(@perm,$i,$s);
my$news=substr($$refin,-1);
for(@{permutations(\substr($$refin,0,-1))}){
for($i=0;$i<length$$refin;$i++){
$s=$_;
substr($s,$i,0,$news);
push@perm,$s;
}
}
return\@perm;
}
use strict;
use warnings;
my@symbols=('a','b','c','d','e');
my$grouping=3;
die"You can't group ",$#symbols+1," symbols $grouping by $grouping!\n"if$grouping>$#symbols;
my$refallperms=permutations_nxr($#symbols,$grouping-1);
for my$p(@$refallperms){
for my$q(0..$grouping-1){
print$symbols[ord(substr($p,$q))],' ';
}
print"\n";
}
print'Number of permutations of ',$#symbols+1," symbols $grouping by $grouping: ",scalar@{$refallperms},"\n";
#####################################################
sub permutations_nxr{
my($n,$r)=@_;
my$refperm=combinations($n,$r);
my@perms;
for(@$refperm){
push@perms,@{permutations(\$_)};
}
return\@perms;
}
#####################################################
sub combinations{
my($n,$r)=@_;
my(@comb,@newc,$i,$j,$c);
local($_);
@comb=map{chr}(0..$n-$r);
for($i=$n-$r+1;$i<=$n;$i++){
for(@comb){
$j=1+ord(substr($_,-1));
last if$j>$i;
$c=$_;
$_.=chr($j);
for($j++;$j<=$i;$j++){
push@newc,$c.chr($j);
}
}
push@comb,@newc;
@newc=();
}
return\@comb;
}
#####################################################
sub permutations{
my($refin)=@_;
#reference to a string containing n different chr's
return[$$refin]if length($$refin)==1;
local($_);
my(@perm,$i,$s);
my$news=substr($$refin,-1);
for(@{permutations(\substr($$refin,0,-1))}){
for($i=0;$i<length$$refin;$i++){
$s=$_;
substr($s,$i,0,$news);
push@perm,$s;
}
}
return\@perm;
}