Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
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 for computing all permutations of n natural numbers.
5///
6/// It can be run with:
7///
8/// ~~~{.cpp}
9/// root > .x permute.C
10/// root > .x permute.C+ with ACLIC
11/// ~~~
12///
13/// \macro_output
14/// \macro_code
15///
16/// \author Federico Carminati
17
18#include <TMath.h>
19
20int permuteSimple1 ()
21{
22 printf("\nTMath::Permute simple test\n");
23 printf("==========================\n");
24 char aa='a';
25 Int_t a[4];
26 Int_t i;
27 Int_t icount=0;
28 for(i=0; i<4; i++) a[i]=i;
29 do {
30 icount++;
31 for(Int_t i=0;i<4;printf("%c",static_cast<char>(aa+a[i++])));
32 printf("\n");
33
34 } while(TMath::Permute(4,a));
35 printf("Found %d permutations = 4!\n",icount);
36 return 0;
37}
38
39int permuteSimple2 ()
40{
41 printf("\nTMath::Permute simple test with repetition\n");
42 printf("==========================================\n");
43 char aa='a'-1;
44 Int_t a[6];
45 Int_t i;
46 Int_t icount=0;
47 for(i=0; i<6; i++) a[i]=(i+2)/2;
48 do {
49 icount++;
50 for(Int_t i=0;i<5;printf("%c",static_cast<char>(aa+a[i++])));
51 printf("\n");
52
53 } while(TMath::Permute(5,a));
54 printf("Found %d permutations = 5!/(2! 2!)\n",icount);
55 return 0;
56}
57
59{
60 Int_t a[10];
61 Int_t &n=a[0], &i=a[1];
62 Int_t &e=a[2], &t=a[3];
63 Int_t &h=a[4], &r=a[5];
64 Int_t &f=a[6], &o=a[7];
65 Int_t &s=a[8], &u=a[9];
67
68 printf("\nTMath::Permute fancy test\n");
69 printf("=========================\n");
70 printf("This is a program to calculate the solution to the following problem\n");
71 printf("Find the equivalence between letters and numbers so that\n\n");
72 printf(" NINE*THREE = NEUF*TROIS\n\n");
73 for(Int_t ii=0; ii<10; ii++) a[ii]=ii;
74 do {
75 nine=((n*10+i)*10+n)*10+e;
76 neuf=((n*10+e)*10+u)*10+f;
77 three=(((t*10+h)*10+r)*10+e)*10+e;
78 trois=(((t*10+r)*10+o)*10+i)*10+s;
79 if(nine*three==neuf*trois) {
80 printf("Solution found!\n\n");
81 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);
82 printf("NINE=%d THREE=%d NEUF=%d TROIS=%d\n",nine,three,neuf,trois);
83 printf("NINE*THREE = NEUF*TROIS = %d\n",neuf*trois);
84 return 0;
85 }
86 } while(TMath::Permute(10,a));
87 printf("No solutions found -- something is wrong here!\n");
88 return 0;
89}
90
91void permute() {
95}
#define f(i)
Definition RSha256.hxx:104
#define a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
#define e(i)
Definition RSha256.hxx:103
int Int_t
Definition RtypesCore.h:45
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
const Int_t n
Definition legend1.C:16
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:2557