# permute¶

Tutorial illustrating the use of TMath::Permute can be run with:

root > .x permute.C
root > .x permute.C+ with ACLIC


Author: Federico Carminati
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, August 17, 2022 at 09:34 AM.

Definition of a helper function:

In [1]:
%%cpp -d

#include <TMath.h>

int permuteSimple1 ()
{
printf("\nTMath::Permute simple test\n");
printf("==========================\n");
char aa='a';
Int_t a[4];
Int_t i;
Int_t icount=0;
for(i=0; i<4; i++) a[i]=i;
do {
icount++;
for(Int_t i=0;i<4;printf("%c",static_cast<char>(aa+a[i++])));
printf("\n");

} while(TMath::Permute(4,a));
printf("Found %d permutations = 4!\n",icount);
return 0;
}


Definition of a helper function:

In [2]:
%%cpp -d
int permuteSimple2 ()
{
printf("\nTMath::Permute simple test with repetition\n");
printf("==========================================\n");
char aa='a'-1;
Int_t a[6];
Int_t i;
Int_t icount=0;
for(i=0; i<6; i++) a[i]=(i+2)/2;
do {
icount++;
for(Int_t i=0;i<5;printf("%c",static_cast<char>(aa+a[i++])));
printf("\n");

} while(TMath::Permute(5,a));
printf("Found %d permutations = 5!/(2! 2!)\n",icount);
return 0;
}


Definition of a helper function:

In [3]:
%%cpp -d
Int_t permuteFancy()
{
Int_t a[10];
Int_t &n=a[0], &i=a[1];
Int_t &e=a[2], &t=a[3];
Int_t &h=a[4], &r=a[5];
Int_t &f=a[6], &o=a[7];
Int_t &s=a[8], &u=a[9];
Int_t nine, three, neuf, trois;

printf("\nTMath::Permute fancy test\n");
printf("=========================\n");
printf("This is a program to calculate the solution to the following problem\n");
printf("Find the equivalence between letters and numbers so that\n\n");
printf("              NINE*THREE = NEUF*TROIS\n\n");
for(Int_t ii=0; ii<10; ii++) a[ii]=ii;
do {
nine=((n*10+i)*10+n)*10+e;
neuf=((n*10+e)*10+u)*10+f;
three=(((t*10+h)*10+r)*10+e)*10+e;
trois=(((t*10+r)*10+o)*10+i)*10+s;
if(nine*three==neuf*trois) {
printf("Solution found!\n\n");
printf("T=%d N=%d E=%d S=%d F=%d H=%d R=%d I=%d O=%d U=%d\n",t,n,e,s,f,h,r,i,o,u);
printf("NINE=%d THREE=%d NEUF=%d TROIS=%d\n",nine,three,neuf,trois);
printf("NINE*THREE = NEUF*TROIS = %d\n",neuf*trois);
return 0;
}
} while(TMath::Permute(10,a));
printf("No solutions found -- something is wrong here!\n");
return 0;
}

In [4]:
permuteSimple1();
permuteSimple2();
permuteFancy();

TMath::Permute simple test
==========================
abcd
abdc
acbd
acdb
bacd
bcda
bdac
bdca
cabd
cbda
cdab
cdba
dabc
dacb
dbac
dbca
dcab
dcba
Found 24 permutations = 4!

TMath::Permute simple test with repetition
==========================================
aabbc
aabcb
aacbb
ababc
abacb
abbac
abbca
abcab
abcba
acabb
acbab
acbba
baabc
baacb
babac
babca
bacab
bacba
bbaac
bbaca
bbcaa
bcaab
bcaba
bcbaa
caabb
cabab
cabba
cbaab
cbaba
cbbaa
Found 30 permutations = 5!/(2! 2!)

TMath::Permute fancy test
=========================
This is a program to calculate the solution to the following problem
Find the equivalence between letters and numbers so that

NINE*THREE = NEUF*TROIS

Solution found!

T=1 N=9 E=6 S=7 F=8 H=3 R=2 I=0 O=5 U=4
NINE=9096 THREE=13266 NEUF=9648 TROIS=12507
NINE*THREE = NEUF*TROIS = 120667536