ROOT logo
// tutorial illustrating the use of TMath::Permute
//  can be run with:
// root > .x permute.C
// root > .x permute.C+ with ACLIC
//Author: Federico Carminati
   
#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;
}

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;
}

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;
}

void permute() {
   permuteSimple1();
   permuteSimple2();
   permuteFancy();
}
 permute.C:1
 permute.C:2
 permute.C:3
 permute.C:4
 permute.C:5
 permute.C:6
 permute.C:7
 permute.C:8
 permute.C:9
 permute.C:10
 permute.C:11
 permute.C:12
 permute.C:13
 permute.C:14
 permute.C:15
 permute.C:16
 permute.C:17
 permute.C:18
 permute.C:19
 permute.C:20
 permute.C:21
 permute.C:22
 permute.C:23
 permute.C:24
 permute.C:25
 permute.C:26
 permute.C:27
 permute.C:28
 permute.C:29
 permute.C:30
 permute.C:31
 permute.C:32
 permute.C:33
 permute.C:34
 permute.C:35
 permute.C:36
 permute.C:37
 permute.C:38
 permute.C:39
 permute.C:40
 permute.C:41
 permute.C:42
 permute.C:43
 permute.C:44
 permute.C:45
 permute.C:46
 permute.C:47
 permute.C:48
 permute.C:49
 permute.C:50
 permute.C:51
 permute.C:52
 permute.C:53
 permute.C:54
 permute.C:55
 permute.C:56
 permute.C:57
 permute.C:58
 permute.C:59
 permute.C:60
 permute.C:61
 permute.C:62
 permute.C:63
 permute.C:64
 permute.C:65
 permute.C:66
 permute.C:67
 permute.C:68
 permute.C:69
 permute.C:70
 permute.C:71
 permute.C:72
 permute.C:73
 permute.C:74
 permute.C:75
 permute.C:76
 permute.C:77
 permute.C:78
 permute.C:79
 permute.C:80
 permute.C:81