{
"cells": [
{
"cell_type": "markdown",
"id": "0e72662e",
"metadata": {},
"source": [
"# permute\n",
"Tutorial illustrating the use of TMath::Permute for computing all permutations of n natural numbers.\n",
"\n",
"It can be run with:\n",
"\n",
"```cpp\n",
"root > .x permute.C\n",
"root > .x permute.C+ with ACLIC\n",
"```\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Federico Carminati \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:27 PM."
]
},
{
"cell_type": "markdown",
"id": "1ba4722a",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f1f0e1d1",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:06.730936Z",
"iopub.status.busy": "2026-05-19T20:27:06.730825Z",
"iopub.status.idle": "2026-05-19T20:27:06.735820Z",
"shell.execute_reply": "2026-05-19T20:27:06.735392Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"\n",
"#include \n",
"\n",
"int permuteSimple1 ()\n",
"{\n",
" printf(\"\\nTMath::Permute simple test\\n\");\n",
" printf(\"==========================\\n\");\n",
" char aa='a';\n",
" Int_t a[4];\n",
" Int_t i;\n",
" Int_t icount=0;\n",
" for(i=0; i<4; i++) a[i]=i;\n",
" do {\n",
" icount++;\n",
" for(Int_t i=0;i<4;printf(\"%c\",static_cast(aa+a[i++])));\n",
" printf(\"\\n\");\n",
"\n",
" } while(TMath::Permute(4,a));\n",
" printf(\"Found %d permutations = 4!\\n\",icount);\n",
" return 0;\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "d4156cdc",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "caca4f0a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:06.736991Z",
"iopub.status.busy": "2026-05-19T20:27:06.736875Z",
"iopub.status.idle": "2026-05-19T20:27:06.740530Z",
"shell.execute_reply": "2026-05-19T20:27:06.740120Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"int permuteSimple2 ()\n",
"{\n",
" printf(\"\\nTMath::Permute simple test with repetition\\n\");\n",
" printf(\"==========================================\\n\");\n",
" char aa='a'-1;\n",
" Int_t a[6];\n",
" Int_t i;\n",
" Int_t icount=0;\n",
" for(i=0; i<6; i++) a[i]=(i+2)/2;\n",
" do {\n",
" icount++;\n",
" for(Int_t i=0;i<5;printf(\"%c\",static_cast(aa+a[i++])));\n",
" printf(\"\\n\");\n",
"\n",
" } while(TMath::Permute(5,a));\n",
" printf(\"Found %d permutations = 5!/(2! 2!)\\n\",icount);\n",
" return 0;\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "9ddf341b",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8fc8e38d",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:06.741712Z",
"iopub.status.busy": "2026-05-19T20:27:06.741576Z",
"iopub.status.idle": "2026-05-19T20:27:06.746007Z",
"shell.execute_reply": "2026-05-19T20:27:06.745600Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"Int_t permuteFancy()\n",
"{\n",
" Int_t a[10];\n",
" Int_t &n=a[0], &i=a[1];\n",
" Int_t &e=a[2], &t=a[3];\n",
" Int_t &h=a[4], &r=a[5];\n",
" Int_t &f=a[6], &o=a[7];\n",
" Int_t &s=a[8], &u=a[9];\n",
" Int_t nine, three, neuf, trois;\n",
"\n",
" printf(\"\\nTMath::Permute fancy test\\n\");\n",
" printf(\"=========================\\n\");\n",
" printf(\"This is a program to calculate the solution to the following problem\\n\");\n",
" printf(\"Find the equivalence between letters and numbers so that\\n\\n\");\n",
" printf(\" NINE*THREE = NEUF*TROIS\\n\\n\");\n",
" for(Int_t ii=0; ii<10; ii++) a[ii]=ii;\n",
" do {\n",
" nine=((n*10+i)*10+n)*10+e;\n",
" neuf=((n*10+e)*10+u)*10+f;\n",
" three=(((t*10+h)*10+r)*10+e)*10+e;\n",
" trois=(((t*10+r)*10+o)*10+i)*10+s;\n",
" if(nine*three==neuf*trois) {\n",
" printf(\"Solution found!\\n\\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);\n",
" printf(\"NINE=%d THREE=%d NEUF=%d TROIS=%d\\n\",nine,three,neuf,trois);\n",
" printf(\"NINE*THREE = NEUF*TROIS = %d\\n\",neuf*trois);\n",
" return 0;\n",
" }\n",
" } while(TMath::Permute(10,a));\n",
" printf(\"No solutions found -- something is wrong here!\\n\");\n",
" return 0;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "be4b392a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:06.747244Z",
"iopub.status.busy": "2026-05-19T20:27:06.747128Z",
"iopub.status.idle": "2026-05-19T20:27:07.066429Z",
"shell.execute_reply": "2026-05-19T20:27:07.066007Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"TMath::Permute simple test\n",
"==========================\n",
"abcd\n",
"abdc\n",
"acbd\n",
"acdb\n",
"adbc\n",
"adcb\n",
"bacd\n",
"badc\n",
"bcad\n",
"bcda\n",
"bdac\n",
"bdca\n",
"cabd\n",
"cadb\n",
"cbad\n",
"cbda\n",
"cdab\n",
"cdba\n",
"dabc\n",
"dacb\n",
"dbac\n",
"dbca\n",
"dcab\n",
"dcba\n",
"Found 24 permutations = 4!\n",
"\n",
"TMath::Permute simple test with repetition\n",
"==========================================\n",
"aabbc\n",
"aabcb\n",
"aacbb\n",
"ababc\n",
"abacb\n",
"abbac\n",
"abbca\n",
"abcab\n",
"abcba\n",
"acabb\n",
"acbab\n",
"acbba\n",
"baabc\n",
"baacb\n",
"babac\n",
"babca\n",
"bacab\n",
"bacba\n",
"bbaac\n",
"bbaca\n",
"bbcaa\n",
"bcaab\n",
"bcaba\n",
"bcbaa\n",
"caabb\n",
"cabab\n",
"cabba\n",
"cbaab\n",
"cbaba\n",
"cbbaa\n",
"Found 30 permutations = 5!/(2! 2!)\n",
"\n",
"TMath::Permute fancy test\n",
"=========================\n",
"This is a program to calculate the solution to the following problem\n",
"Find the equivalence between letters and numbers so that\n",
"\n",
" NINE*THREE = NEUF*TROIS\n",
"\n",
"Solution found!\n",
"\n",
"T=1 N=9 E=6 S=7 F=8 H=3 R=2 I=0 O=5 U=4\n",
"NINE=9096 THREE=13266 NEUF=9648 TROIS=12507\n",
"NINE*THREE = NEUF*TROIS = 120667536\n"
]
}
],
"source": [
"permuteSimple1();\n",
"permuteSimple2();\n",
"permuteFancy();"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ROOT C++",
"language": "c++",
"name": "root"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".C",
"mimetype": " text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 5
}