Logo ROOT  
Reference Guide
permute.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_math
3/// \notebook -nodraw
4/// Tutorial illustrating the use of TMath::Permute
5/// can be run with:
6///
7/// ~~~{.cpp}
8/// root > .x permute.C
9/// root > .x permute.C+ with ACLIC
10/// ~~~
11///
12/// \macro_output
13/// \macro_code
14///
15/// \author Federico Carminati
16
17#include <TMath.h>
18
19int permuteSimple1 ()
20{
21 printf("\nTMath::Permute simple test\n");
22 printf("==========================\n");
23 char aa='a';
24 Int_t a[4];
25 Int_t i;
26 Int_t icount=0;
27 for(i=0; i<4; i++) a[i]=i;
28 do {
29 icount++;
30 for(Int_t i=0;i<4;printf("%c",static_cast<char>(aa+a[i++])));
31 printf("\n");
32
33 } while(TMath::Permute(4,a));
34 printf("Found %d permutations = 4!\n",icount);
35 return 0;
36}
37
38int permuteSimple2 ()
39{
40 printf("\nTMath::Permute simple test with repetition\n");
41 printf("==========================================\n");
42 char aa='a'-1;
43 Int_t a[6];
44 Int_t i;
45 Int_t icount=0;
46 for(i=0; i<6; i++) a[i]=(i+2)/2;
47 do {
48 icount++;
49 for(Int_t i=0;i<5;printf("%c",static_cast<char>(aa+a[i++])));
50 printf("\n");
51
52 } while(TMath::Permute(5,a));
53 printf("Found %d permutations = 5!/(2! 2!)\n",icount);
54 return 0;
55}
56
57Int_t permuteFancy()
58{
59 Int_t a[10];
60 Int_t &n=a[0], &i=a[1];
61 Int_t &e=a[2], &t=a[3];
62 Int_t &h=a[4], &r=a[5];
63 Int_t &f=a[6], &o=a[7];
64 Int_t &s=a[8], &u=a[9];
65 Int_t nine, three, neuf, trois;
66
67 printf("\nTMath::Permute fancy test\n");
68 printf("=========================\n");
69 printf("This is a program to calculate the solution to the following problem\n");
70 printf("Find the equivalence between letters and numbers so that\n\n");
71 printf(" NINE*THREE = NEUF*TROIS\n\n");
72 for(Int_t ii=0; ii<10; ii++) a[ii]=ii;
73 do {
74 nine=((n*10+i)*10+n)*10+e;
75 neuf=((n*10+e)*10+u)*10+f;
76 three=(((t*10+h)*10+r)*10+e)*10+e;
77 trois=(((t*10+r)*10+o)*10+i)*10+s;
78 if(nine*three==neuf*trois) {
79 printf("Solution found!\n\n");
80 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);
81 printf("NINE=%d THREE=%d NEUF=%d TROIS=%d\n",nine,three,neuf,trois);
82 printf("NINE*THREE = NEUF*TROIS = %d\n",neuf*trois);
83 return 0;
84 }
85 } while(TMath::Permute(10,a));
86 printf("No solutions found -- something is wrong here!\n");
87 return 0;
88}
89
90void permute() {
91 permuteSimple1();
92 permuteSimple2();
93 permuteFancy();
94}
ROOT::R::TRInterface & r
Definition: Object.C:4
#define f(i)
Definition: RSha256.hxx:104
#define h(i)
Definition: RSha256.hxx:106
#define e(i)
Definition: RSha256.hxx:103
int Int_t
Definition: RtypesCore.h:43
const Int_t n
Definition: legend1.C:16
static constexpr double s
Bool_t Permute(Int_t n, Int_t *a)
Simple recursive algorithm to find the permutations of n natural numbers, not necessarily all distinc...
Definition: TMath.cxx:2517
auto * a
Definition: textangle.C:12