summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/profcollege/latex/PfCLabyrintheNombre.tex
blob: be5e05d6731819aed5097aad7b09b9e2d2f4266d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
%%%
% Labyrinthe Nombre
%%%
\setKVdefault[ClesLabyNb]{Multiple=5,XDepart=0,YDepart=0,Longueur=7,Largeur=4,Solution=false,Echelle=1,Angle=0,Couleur=red,Murs=false,Nom=Ex1,CouleurChemin=LightSteelBlue,DoubleOperation=false,Multiplication=false,Chemin=false,EntreeSortie=false,Entree="Nord",Sortie="Sud",SortieUnique=true,Graines=false,XArrivees=false,YArrivees=false,ArriveeX=5,ArriveeY=5}
\defKV[ClesLabyNb]{Graine=\setKV[ClesLabyNb]{Graines}}
\defKV[ClesLabyNb]{XArrivee=\setKV[ClesLabyNb]{XArrivees}\setKV[ClesLabyNb]{ArriveeX=#1}}
\defKV[ClesLabyNb]{YArrivee=\setKV[ClesLabyNb]{YArrivees}\setKV[ClesLabyNb]{ArriveeY=#1}}

\newcommand\LabyNombre[1][]{%
  \useKVdefault[ClesLabyNb]%
  \setKV[ClesLabyNb]{#1}%
  \ifboolKV[ClesLabyNb]{XArrivees}{}{\setKV[ClesLabyNb]{ArriveeX=\fpeval{\useKV[ClesLabyNb]{Longueur}-1}}}
  \ifboolKV[ClesLabyNb]{YArrivees}{}{\setKV[ClesLabyNb]{ArriveeY=\fpeval{\useKV[ClesLabyNb]{Largeur}-1}}}
  \ifboolKV[ClesLabyNb]{Multiplication}{%
    \TraceLabyMul{\useKV[ClesLabyNb]{Multiple}}{\useKV[ClesLabyNb]{Longueur}}{\useKV[ClesLabyNb]{Largeur}}{(\useKV[ClesLabyNb]{YDepart},\useKV[ClesLabyNb]{XDepart})}{(\useKV[ClesLabyNb]{ArriveeY},\useKV[ClesLabyNb]{ArriveeX})}%
  }{%
    \ifboolKV[ClesLabyNb]{Solution}{%
      \TraceLabyFactoSolution{\useKV[ClesLabyNb]{Multiple}}{\useKV[ClesLabyNb]{Longueur}}{\useKV[ClesLabyNb]{Largeur}}{(\useKV[ClesLabyNb]{YDepart},\useKV[ClesLabyNb]{XDepart})}{(\useKV[ClesLabyNb]{ArriveeY},\useKV[ClesLabyNb]{ArriveeX})}{\useKV[ClesLabyNb]{Solution}}%
    }{%
      \TraceLabyFacto{\useKV[ClesLabyNb]{Multiple}}{\useKV[ClesLabyNb]{Longueur}}{\useKV[ClesLabyNb]{Largeur}}{(\useKV[ClesLabyNb]{YDepart},\useKV[ClesLabyNb]{XDepart})}{(\useKV[ClesLabyNb]{ArriveeY},\useKV[ClesLabyNb]{ArriveeX})}{\useKV[ClesLabyNb]{Solution}}%
    }%
  }%
}%

\newcommand\TraceLabyMul[5]{
  \ifluatex
  \mplibnumbersystem{double}
  \mplibforcehmode
  \begin{mplibcode}
  input PfCLabyNombre;
  
  boolean Murs,Solution,Graines;
  Murs=\useKV[ClesLabyNb]{Murs};
  Solution:=\useKV[ClesLabyNb]{Solution};
  Graines:=\useKV[ClesLabyNb]{Graines};
  if Graines:
  randomseed:=\useKV[ClesLabyNb]{Graine}
  fi;

  path Fullcircle;
  Fullcircle=fullcircle shifted((0.5,0.5));
  
  numeric Multiple;
  Multiple=#1;
  % Initialisation du labyrinthe
  InitialisationLabyrintheMul(#2,#3);
  % On initialise les paramètres du parcours
  numeric choixligneD,choixligneA,choixcolonneD,choixcolonneA;
  choixligneD=xpart(#4);
  choixcolonneD=ypart(#4);
  choixligneA=xpart(#5);
  choixcolonneA=ypart(#5);%
  pair Depart;
  Depart=N[choixligneD][choixcolonneD];
  pair Arrivee;
  Arrivee=N[choixligneA][choixcolonneA];
  pair Mobile;
  Mobile=Depart;
  RAZPileChemin;
  % Exploration du labyrinthe
  PushChemin((choixligneD,choixcolonneD));
  CaseExploree[choixligneD][choixcolonneD]:=true;
  VoisinDispo(choixligneD,choixcolonneD);
  forever: exitif Mobile=Arrivee;%nb=0;
  nb:=ceiling(uniformdeviate(nbvoisin));
  if nb>0:
  for k=1 upto nbvoisin:
  CaseExploree[xpart(PileVoisin[k])][ypart(PileVoisin[k])]:=true;
  endfor;
  PushChemin((xpart(PileVoisin[nb]),ypart(PileVoisin[nb])));
  Mobile:=N[xpart(PileChemin[indiceChemin])][ypart(PileChemin[indiceChemin])];
  VoisinDispo(xpart(PileChemin[indiceChemin]),ypart(PileChemin[indiceChemin]));
  else:
  PopChemin;
  fi;
  endfor;

  vardef PremierInfCent=
    Totalpremier:=25;

    numeric p[]; boolean n_is_prime; p[1]=2; kis:=1;
    for nis=3 step 2 until infinity:
    n_is_prime := true;
    for jis=2 upto kis:
    if nis mod p[jis]=0: n_is_prime := false; fi
    exitif nis/p[jis] < p[jis];
    endfor
    if n_is_prime: p[incr kis] := nis; exitif kis=Totalpremier; fi
    endfor fi
    
    numeric PR[],pretiens[];
    
    for ki=1 upto Totalpremier-1:
    pretiens[ki]:=p[ki+1];
    endfor;
    
    Kk:=0;
    
    for ki=Totalpremier-1 downto 1:
    alea:=1+floor(uniformdeviate(ki));
    PR[incr Kk]:=pretiens[alea];
    Kkl:=0;
    for li=1 upto ki:
    if li<>alea:
      pretiens[incr Kkl]:=pretiens[li];
      fi;
    endfor;
    endfor;
    enddef;
  
    vardef ListeDiviseurs(expr nbtest)=
    numeric diviseurs[],nondiviseurs[],pas;
    nbdiv:=0;
    nbnondiv:=0;
    pas=2;
    forever:
    if (nbtest mod pas)=0:
    nbdiv:=nbdiv+1;
    diviseurs[nbdiv]=pas;
    else:
    nbnondiv:=nbnondiv+1;
    nondiviseurs[nbnondiv]=pas;
    fi;
    pas:=pas+1;
    exitif pas>10;
    endfor;
    enddef;    
    
  % Affichagefinal
  % on sauvegarde les nombres aléatoires
    numeric NbAffiche[];
    numeric NbSol[];
    % on écrit des nombres premiers au hasard >10
    PremierInfCent;
    numeric nbaffiche;
    nbaffiche=0;
    for k=0 upto LargeurLaby-1:
      for l=0 upto LongueurLaby-1:
      nbaffiche:=nbaffiche+1;
      if (nbaffiche mod 2)=0:
      nbpremier:=3+ceiling(uniformdeviate(Totalpremier-4));
      NbAffiche[nbaffiche]:=PR[nbpremier];
      else:
      NbAffiche[nbaffiche]:=2+floor(uniformdeviate(8));
      fi;
      endfor;
    endfor;
    % On crée des multiples du nombre choisi
    NbSol[1]=Multiple;
    for k=2 upto indiceChemin:
    if (k mod 2)=0:
    NbSol[k]=NbSol[k-1]*(2+ceiling(uniformdeviate(8)));
    else:
    % Diviseurs du précédent sauf 1 mais inférieur ou égal à 10
    % choix aléatoire du diviseur
    ListeDiviseurs(NbSol[k-1]);
    choixdiv:=ceiling(uniformdeviate(nbdiv));
    NbSol[k]=diviseurs[choixdiv];
    fi;
    endfor;
  % On affiche
  picture Corps;
  Corps=image(
  drawoptions(withcolor 0.7white);
  for k=0 upto LargeurLaby-1:
  trace (N[k][0]--N[k][LongueurLaby-1]) shifted((5mm,0.25*uv));
  endfor;
  for k=0 upto LongueurLaby-1:
  trace (N[0][k]--N[LargeurLaby-1][k]) shifted((5mm,0.25*uv));
  endfor;
  drawoptions();
  nbaffiche:=0;
  for k=0 upto LargeurLaby-1:
  for l=0 upto LongueurLaby-1:
  nbaffiche:=nbaffiche+1;
    remplis ((Fullcircle scaled 10mm) shifted N[k][l]) withcolor white;
  label(TEX("\num{"&decimal(NbAffiche[nbaffiche])&"}"),M[k][l]);
  endfor;
  endfor;
  if Solution:
  trace ((center Fullcircle scaled 10mm) shifted N[choixligneD][choixcolonneD])
  for k=2 upto indiceChemin-1:
  -- ((center Fullcircle scaled 10mm) shifted N[xpart(PileChemin[k])][ypart(PileChemin[k])])
  endfor
  --((center Fullcircle scaled 10mm) shifted Arrivee) dashed evenly withpen pencircle scaled 1.5 withcolor Brown;
  fi;
  remplis ((Fullcircle scaled 10mm) shifted N[choixligneD][choixcolonneD]) withcolor \useKV[ClesLabyNb]{Couleur};
  if (LargeurLaby mod 2)=0:
  remplis ((unitsquare scaled 10mm) shifted Arrivee) withcolor \useKV[ClesLabyNb]{Couleur};
  else:
  remplis ((Fullcircle scaled 10mm) shifted Arrivee) withcolor \useKV[ClesLabyNb]{Couleur};
  fi;
  for k=2 upto indiceChemin-1:
  remplis ((Fullcircle scaled 10mm) shifted N[xpart(PileChemin[k])][ypart(PileChemin[k])]) withcolor white;
  label(TEX("\num{"&decimal(NbSol[k])&"}"),M[xpart(PileChemin[k])][ypart(PileChemin[k])]);
  endfor;
  k:=1;
  label(TEX("\num{"&decimal(NbSol[k])&"}"),M[xpart(PileChemin[k])][ypart(PileChemin[k])]);
  k:=indiceChemin;
  label(TEX("\num{"&decimal(NbSol[k])&"}"),M[xpart(PileChemin[k])][ypart(PileChemin[k])]);
  trace TraceLabyrintheMul;
  );
  Corps:=(Corps scaled \useKV[ClesLabyNb]{Echelle}) rotated \useKV[ClesLabyNb]{Angle};
  trace Corps;
\end{mplibcode}
\fi
}

\newcommand\TraceLabyFacto[6]{%
  \mplibforcehmode%
  \xdef\PfCNomLabyrinthe{\useKV[ClesLabyNb]{Nom}}%
  \begin{mplibcode}[\PfCNomLabyrinthe]
    input PfCLabyNombre;

    boolean Murs;
    Murs=\useKV[ClesLabyNb]{Murs};

    string Entree,Sortie;
    Entree=\useKV[ClesLabyNb]{Entree};
    Sortie=\useKV[ClesLabyNb]{Sortie};
    boolean SortieUnique,EntreeSortie;
    SortieUnique:=\useKV[ClesLabyNb]{SortieUnique};
    EntreeSortie:=\useKV[ClesLabyNb]{EntreeSortie};
    picture Square;
    Square=image(
    path Test;
    Test=unitsquare scaled 10mm;
    trace subpath(0.85,1.15) of Test;
    trace subpath(1.85,2.15) of Test;
    trace subpath(2.85,3.15) of Test;
    trace subpath(3.85,4.15) of Test;
    );

    vardef RemplissagePremier=
    Totalpremier:=if indiceChemin<62:62 else: indiceChemin fi;

    numeric p[]; boolean n_is_prime; p[1]=2; kis:=1;
    for nis=3 step 2 until infinity:
    n_is_prime := true;
    for jis=2 upto kis:
    if nis mod p[jis]=0: n_is_prime := false; fi
    exitif nis/p[jis] < p[jis];
    endfor
    if n_is_prime: p[incr kis] := nis; exitif kis=Totalpremier; fi
    endfor fi
    
    numeric PR[],pretiens[];
    
    for ki=1 upto Totalpremier-1:
    pretiens[ki]:=p[ki+1];
    endfor;
    
    Kk:=0;
    
    for ki=Totalpremier-1 downto 1:
    alea:=1+floor(uniformdeviate(ki));
    PR[incr Kk]:=pretiens[alea];
    % message("k="&decimal(k)&" alea="&decimal(alea)&" Kk="&decimal(Kk)&" PR[Kk]="&decimal(PR[Kk]));
    Kkl:=0;
    for li=1 upto ki:
    if li<>alea:
      pretiens[incr Kkl]:=pretiens[li];
      % message("Kkl="&decimal(Kkl));
      fi;
    endfor;
    endfor;
    enddef;
    
    numeric Multiple;
    Multiple=#1;
    % Initialisation du labyrinthe
    InitialisationLabyrinthe(#2,#3);
    % On initialise les paramètres du parcours
    numeric choixligneD,choixligneA,choixcolonneD,choixcolonneA;
    choixligneD=xpart(#4);
    choixcolonneD=ypart(#4);
    choixligneA=xpart(#5);
    choixcolonneA=ypart(#5);%
    pair Depart;
    Depart=N[choixligneD][choixcolonneD];
    pair Arrivee;
    Arrivee=N[choixligneA][choixcolonneA];
    pair Mobile;
    Mobile=Depart;
    %
    RAZPileChemin;

    if \useKV[ClesLabyNb]{SortieUnique}:
    if (Sortie="Est") or (Sortie="Ouest"):
    for k=0 upto LargeurLaby-1:
    if k<>choixligneA:
    CaseExploree[k][choixcolonneA]:=true;
    fi;
    endfor;
    elseif (Sortie="Nord") or (Sortie="Sud"):
    for k=0 upto LongueurLaby-1:
    if k<>choixcolonneA:
    CaseExploree[choixligneA][k]:=true;
    fi;
    endfor;
    fi;
    fi;
    % Exploration du labyrinthe
    if Entree="Nord":
    CaseExploree[choixligneD-1][choixcolonneD]:=true;
    elseif Entree="Sud":
    CaseExploree[choixligneD+1][choixcolonneD]:=true;
    elseif Entree="Est":
    CaseExploree[choixligneD][choixcolonneD+1]:=true;
    elseif Entree="Ouest":
    CaseExploree[choixligneD][choixcolonneD-1]:=true;
    fi;
    %
    PushChemin((choixligneD,choixcolonneD));
    CaseExploree[choixligneD][choixcolonneD]:=true;
    VoisinDispo(choixligneD,choixcolonneD);
    forever: exitif Mobile=Arrivee;%nb=0;
    nb:=ceiling(uniformdeviate(nbvoisin));
    if nb>0:
      for k=1 upto nbvoisin:
        CaseExploree[xpart(PileVoisin[k])][ypart(PileVoisin[k])]:=true;
      endfor;
      PushChemin((xpart(PileVoisin[nb]),ypart(PileVoisin[nb])));
      Mobile:=N[xpart(PileChemin[indiceChemin])][ypart(PileChemin[indiceChemin])];
      VoisinDispo(xpart(PileChemin[indiceChemin]),ypart(PileChemin[indiceChemin]));
    else:
      PopCheminMathAlea;
    fi;
    endfor;
    % Affichagefinal
    % on sauvegarde les nombres aléatoires
    numeric NbAffiche[];
    numeric NbSol[];
    % on écrit des nombres au hasard, mais sans être multiple du nombre choisi
    numeric nbaffiche;
    nbaffiche=0;
    for k=0 upto LargeurLaby-1:
      for l=0 upto LongueurLaby-1:
      nbaffiche:=nbaffiche+1;
      if Multiple=1:%On affichera que des multiples de 2/3/5/9/10 pour afficher un chemin de nombre premiers
      mathalea:=uniformdeviate(5);
      if mathalea<1:
      NbAffiche[nbaffiche]:=(10+ceiling(uniformdeviate(100)))*2;
      elseif mathalea<2:
      NbAffiche[nbaffiche]:=(10+ceiling(uniformdeviate(100)))*3;
      elseif mathalea<3:
      NbAffiche[nbaffiche]:=(10+ceiling(uniformdeviate(100)))*5;
      elseif mathalea<4:
      NbAffiche[nbaffiche]:=(10+ceiling(uniformdeviate(100)))*9;
      else:  NbAffiche[nbaffiche]:=(10+ceiling(uniformdeviate(100)))*10;
      fi;
      else:
      NbAffiche[nbaffiche]:=(50+ceiling(uniformdeviate(100)))*Multiple+ceiling(uniformdeviate(Multiple-1));
      fi;
      endfor;
    endfor;
    % On crée des multiples du nombre choisi
    RemplissagePremier;
    
    for k=1 upto indiceChemin:
    if Multiple>1:
    NbSol[k]=(50+ceiling(uniformdeviate(100)))*Multiple;
    else:
    NbSol[k]=PR[k];
    fi;
    endfor;
    % On affiche
    picture Corps;
    Corps=image(
      nbaffiche:=0;
      for k=0 upto LargeurLaby-1:
        for l=0 upto LongueurLaby-1:
	  nbaffiche:=nbaffiche+1;
          label(TEX("\num{"&decimal(NbAffiche[nbaffiche])&"}"),M[k][l]);
        endfor;
      endfor;
      for k=1 upto indiceChemin:
      remplis ((unitsquare scaled 10mm) shifted N[xpart(PileChemin[k])][ypart(PileChemin[k])]) withcolor white;
      label(TEX("\num{"&decimal(NbSol[k])&"}"),M[xpart(PileChemin[k])][ypart(PileChemin[k])]);
      endfor;
      if EntreeSortie=false:
      remplis ((unitsquare scaled 10mm) shifted N[choixligneD][choixcolonneD]) withcolor \useKV[ClesLabyNb]{Couleur};
      remplis ((unitsquare scaled 10mm) shifted Arrivee) withcolor \useKV[ClesLabyNb]{Couleur};
      trace TraceLabyrinthe;
      else:
      for k=0 upto LargeurLaby-1:
      for l=0 upto LongueurLaby-1:
      trace Square shifted N[k][l];
      endfor;
      endfor;
      trace u*(0,0)--u*(LongueurLaby,0)--u*(LongueurLaby,-LargeurLaby)--u*(0,-LargeurLaby)--cycle;
      % On dessine l'entrée.
      if Entree="Nord":
      N[-1][-1]:=Depart+(0,10mm);
      fill ((unitsquare scaled 10mm) shifted N[-1][-1]) withcolor white;
      draw (subpath(1,4) of unitsquare scaled 10mm) shifted N[-1][-1];
      undraw (subpath(0.18,0.82) of unitsquare scaled 10mm) shifted N[-1][-1] withpen pencircle scaled 1.25;
      label(btex \footnotesize Départ etex,(center Square) shifted N[-1][-1]);
      elseif Entree="Ouest":
      N[-1][-1]:=Depart+(-10mm,0);
      fill ((unitsquare scaled 10mm) shifted N[-1][-1]) withcolor white;
      draw (subpath(2,5) of unitsquare scaled 10mm) shifted N[-1][-1];
      undraw (subpath(1.15,1.85) of unitsquare scaled 10mm) shifted N[-1][-1] withpen pencircle scaled 1.25;
      label(btex \footnotesize Départ etex,(center Square) shifted N[-1][-1]);
      elseif Entree="Sud":
      N[-1][-1]:=Depart+(0,-10mm);
      fill ((unitsquare scaled 10mm) shifted N[-1][-1]) withcolor white;
      draw (subpath(3,6) of unitsquare scaled 10mm) shifted N[-1][-1];
      undraw (subpath(2.15,2.85) of unitsquare scaled 10mm) shifted N[-1][-1] withpen pencircle scaled 1.25;
      label(btex \footnotesize Départ etex,(center Square) shifted N[-1][-1]);
      elseif Entree="Est":
      N[-1][-1]:=Depart+(10mm,0);
      fill ((unitsquare scaled 10mm) shifted N[-1][-1]) withcolor white;
      draw (subpath(0,3) of unitsquare scaled 10mm) shifted N[-1][-1];
      undraw (subpath(3.18,3.82) of unitsquare scaled 10mm) shifted N[-1][-1] withpen pencircle scaled 1.25;
      label(btex \footnotesize Départ etex,(center Square) shifted N[-1][-1]);
      fi;
      % On dessine les sorties
      if Sortie="Nord":
      N[-2][-2]=Arrivee+(5mm,20mm);
      for k=0 upto LongueurLaby-1:
      draw ((point(0.15) of unitsquare scaled 10mm)--0.5[(point(0.15) of unitsquare scaled 10mm),(point(2.85) of unitsquare scaled 10mm)]) shifted (N[0][k]+(0,10mm));
      draw ((point(0.85) of unitsquare scaled 10mm)--0.5[(point(0.85) of unitsquare scaled 10mm),(point(2.15) of unitsquare scaled 10mm)]) shifted (N[0][k]+(0,10mm));
      undraw (subpath(0.18,0.82) of unitsquare scaled 10mm) shifted (N[0][k]+(0,10mm)) withpen pencircle scaled 1.25;
      label(TEX("\footnotesize S"&decimal(k)),center (Square) shifted (N[0][k]+(0,10mm)));
      endfor;
      elseif Sortie="Ouest":
      N[-2][-2]=Arrivee+(-10mm,5mm);
      for k=0 upto LargeurLaby-1:
      draw ((point(1.15) of unitsquare scaled 10mm)--0.5[(point(1.15) of unitsquare scaled 10mm),(point(3.85) of unitsquare scaled 10mm)]) shifted (N[k][0]+(-10mm,0));
      draw ((point(1.85) of unitsquare scaled 10mm)--0.5[(point(1.85) of unitsquare scaled 10mm),(point(3.15) of unitsquare scaled 10mm)]) shifted (N[k][0]+(-10mm,0));
      undraw (subpath(1.18,1.82) of unitsquare scaled 10mm) shifted (N[k][0]+(-10mm,0)) withpen pencircle scaled 1.25;
      label(TEX("\footnotesize S"&decimal(k)),center (Square) shifted (N[k][0]+(-10mm,0)));
      endfor;
      elseif Sortie="Sud":
      N[-2][-2]=Arrivee+(5mm,-10mm);
      for k=0 upto LongueurLaby-1:
      draw ((point(2.85) of unitsquare scaled 10mm)--0.5[(point(0.15) of unitsquare scaled 10mm),(point(2.85) of unitsquare scaled 10mm)]) shifted (N[LargeurLaby-1][k]+(0,-10mm));
      draw ((point(2.15) of unitsquare scaled 10mm)--0.5[(point(0.85) of unitsquare scaled 10mm),(point(2.15) of unitsquare scaled 10mm)]) shifted (N[LargeurLaby-1][k]+(0,-10mm));
      undraw (subpath(2.18,2.82) of unitsquare scaled 10mm) shifted (N[LargeurLaby-1][k]+(0,-10mm)) withpen pencircle scaled 1.25;
      label(TEX("\footnotesize S"&decimal(k)),center (Square) shifted (N[LargeurLaby-1][k]+(0,-10mm)));
      endfor;
      elseif Sortie="Est":
      N[-2][-2]=Arrivee+(20mm,5mm);
      for k=0 upto LargeurLaby-1:
      draw ((point(3.15) of unitsquare scaled 10mm)--0.5[(point(3.15) of unitsquare scaled 10mm),(point(1.85) of unitsquare scaled 10mm)]) shifted (N[k][LongueurLaby-1]+(10mm,0));
      draw ((point(3.85) of unitsquare scaled 10mm)--0.5[(point(3.85) of unitsquare scaled 10mm),(point(1.15) of unitsquare scaled 10mm)]) shifted (N[k][LongueurLaby-1]+(10mm,0));
      undraw (subpath(3.18,3.8) of unitsquare scaled 10mm) shifted (N[k][LongueurLaby-1]+(10mm,0)) withpen pencircle scaled 1.25;
      label(TEX("\footnotesize S"&decimal(k)),center (Square) shifted (N[k][LongueurLaby-1]+(10mm,0)));
      endfor;
      fi;
      fi;
      );
    Corps:=(Corps scaled \useKV[ClesLabyNb]{Echelle}) rotated \useKV[ClesLabyNb]{Angle};
    trace Corps;
  \end{mplibcode}
}

\newcommand\TraceLabyFactoSolution[6]{%
  \mplibforcehmode%
  \xdef\PfCNomLabyrinthe{\useKV[ClesLabyNb]{Nom}}%
  \begin{mplibcode}[\PfCNomLabyrinthe]
    color CouleurChemin;
    CouleurChemin:=\useKV[ClesLabyNb]{CouleurChemin};
    picture CorpsSolution;
    CorpsSolution=image(
    nbaffiche:=0;
      for k=0 upto LargeurLaby-1:
        for l=0 upto LongueurLaby-1:
          nbaffiche:=nbaffiche+1;
          label(TEX("\num{"&decimal(NbAffiche[nbaffiche])&"}"),M[k][l]);
        endfor;
      endfor;
      if EntreeSortie=false:
            remplis ((unitsquare scaled 10mm) shifted N[choixligneD][choixcolonneD]) withcolor \useKV[ClesLabyNb]{Couleur};
      remplis ((unitsquare scaled 10mm) shifted Arrivee) withcolor \useKV[ClesLabyNb]{Couleur};
      for k=2 upto indiceChemin-1:
        remplis ((unitsquare scaled 10mm) shifted N[xpart(PileChemin[k])][ypart(PileChemin[k])]) withcolor \useKV[ClesLabyNb]{CouleurChemin};
        label(TEX("\num{"&decimal(NbSol[k])&"}"),M[xpart(PileChemin[k])][ypart(PileChemin[k])]);
        endfor;
        else:
      for k=1 upto indiceChemin:
      remplis ((unitsquare scaled 10mm) shifted N[xpart(PileChemin[k])][ypart(PileChemin[k])]) withcolor white;
      label(TEX("\num{"&decimal(NbSol[k])&"}"),M[xpart(PileChemin[k])][ypart(PileChemin[k])]);
      endfor;
      fi;
      if EntreeSortie=false:
      trace TraceLabyrinthe;
      else:
      for k=0 upto LargeurLaby-1:
      for l=0 upto LongueurLaby-1:
      trace Square shifted N[k][l];
      endfor;
      endfor;
      trace u*(0,0)--u*(LongueurLaby,0)--u*(LongueurLaby,-LargeurLaby)--u*(0,-LargeurLaby)--cycle;
      % On dessine l'entrée.
      if Entree="Nord":
      N[-1][-1]:=Depart+(0,10mm);
      fill ((unitsquare scaled 10mm) shifted N[-1][-1]) withcolor white;
      draw (subpath(1,4) of unitsquare scaled 10mm) shifted N[-1][-1];
      undraw (subpath(0.18,0.82) of unitsquare scaled 10mm) shifted N[-1][-1] withpen pencircle scaled 1.25;
      label(btex \footnotesize Départ etex,(center Square) shifted N[-1][-1]);
      elseif Entree="Ouest":
      N[-1][-1]:=Depart+(-10mm,0);
      fill ((unitsquare scaled 10mm) shifted N[-1][-1]) withcolor white;
      draw (subpath(2,5) of unitsquare scaled 10mm) shifted N[-1][-1];
      undraw (subpath(1.15,1.85) of unitsquare scaled 10mm) shifted N[-1][-1] withpen pencircle scaled 1.25;
      label(btex \footnotesize Départ etex,(center Square) shifted N[-1][-1]);
      elseif Entree="Sud":
      N[-1][-1]:=Depart+(0,-10mm);
      fill ((unitsquare scaled 10mm) shifted N[-1][-1]) withcolor white;
      draw (subpath(3,6) of unitsquare scaled 10mm) shifted N[-1][-1];
      undraw (subpath(2.15,2.85) of unitsquare scaled 10mm) shifted N[-1][-1] withpen pencircle scaled 1.25;
      label(btex \footnotesize Départ etex,(center Square) shifted N[-1][-1]);
      elseif Entree="Est":
      N[-1][-1]:=Depart+(10mm,0);
      fill ((unitsquare scaled 10mm) shifted N[-1][-1]) withcolor white;
      draw (subpath(0,3) of unitsquare scaled 10mm) shifted N[-1][-1];
      undraw (subpath(3.18,3.82) of unitsquare scaled 10mm) shifted N[-1][-1] withpen pencircle scaled 1.25;
      label(btex \footnotesize Départ etex,(center Square) shifted N[-1][-1]);
      fi;
      % On dessine les sorties
      if Sortie="Nord":
      N[-2][-2]=Arrivee+(5mm,20mm);
      for k=0 upto LongueurLaby-1:
      draw ((point(0.15) of unitsquare scaled 10mm)--0.5[(point(0.15) of unitsquare scaled 10mm),(point(2.85) of unitsquare scaled 10mm)]) shifted (N[0][k]+(0,10mm));
      draw ((point(0.85) of unitsquare scaled 10mm)--0.5[(point(0.85) of unitsquare scaled 10mm),(point(2.15) of unitsquare scaled 10mm)]) shifted (N[0][k]+(0,10mm));
      undraw (subpath(0.18,0.82) of unitsquare scaled 10mm) shifted (N[0][k]+(0,10mm)) withpen pencircle scaled 1.25;
      label(TEX("\footnotesize S"&decimal(k)),center (Square) shifted (N[0][k]+(0,10mm)));
      endfor;
      elseif Sortie="Ouest":
      N[-2][-2]=Arrivee+(-10mm,5mm);
      for k=0 upto LargeurLaby-1:
      draw ((point(1.15) of unitsquare scaled 10mm)--0.5[(point(1.15) of unitsquare scaled 10mm),(point(3.85) of unitsquare scaled 10mm)]) shifted (N[k][0]+(-10mm,0));
      draw ((point(1.85) of unitsquare scaled 10mm)--0.5[(point(1.85) of unitsquare scaled 10mm),(point(3.15) of unitsquare scaled 10mm)]) shifted (N[k][0]+(-10mm,0));
      undraw (subpath(1.18,1.82) of unitsquare scaled 10mm) shifted (N[k][0]+(-10mm,0)) withpen pencircle scaled 1.25;
      label(TEX("\footnotesize S"&decimal(k)),center (Square) shifted (N[k][0]+(-10mm,0)));
      endfor;
      elseif Sortie="Sud":
      N[-2][-2]=Arrivee+(5mm,-10mm);
      for k=0 upto LongueurLaby-1:
      % draw (Square) shifted (N[LargeurLaby-1][k]+(0,-10mm)) withcolor red;
      draw ((point(2.85) of unitsquare scaled 10mm)--0.5[(point(0.15) of unitsquare scaled 10mm),(point(2.85) of unitsquare scaled 10mm)]) shifted (N[LargeurLaby-1][k]+(0,-10mm));
      draw ((point(2.15) of unitsquare scaled 10mm)--0.5[(point(0.85) of unitsquare scaled 10mm),(point(2.15) of unitsquare scaled 10mm)]) shifted (N[LargeurLaby-1][k]+(0,-10mm));
      undraw (subpath(2.18,2.82) of unitsquare scaled 10mm) shifted (N[LargeurLaby-1][k]+(0,-10mm)) withpen pencircle scaled 1.25;
      label(TEX("\footnotesize S"&decimal(k)),center (Square) shifted (N[LargeurLaby-1][k]+(0,-10mm)));
      endfor;
      elseif Sortie="Est":
      N[-2][-2]=Arrivee+(20mm,5mm);
      for k=0 upto LargeurLaby-1:
      draw ((point(3.15) of unitsquare scaled 10mm)--0.5[(point(3.15) of unitsquare scaled 10mm),(point(1.85) of unitsquare scaled 10mm)]) shifted (N[k][LongueurLaby-1]+(10mm,0));
      draw ((point(3.85) of unitsquare scaled 10mm)--0.5[(point(3.85) of unitsquare scaled 10mm),(point(1.15) of unitsquare scaled 10mm)]) shifted (N[k][LongueurLaby-1]+(10mm,0));
      undraw (subpath(3.18,3.8) of unitsquare scaled 10mm) shifted (N[k][LongueurLaby-1]+(10mm,0)) withpen pencircle scaled 1.25;
      label(TEX("\footnotesize S"&decimal(k)),center (Square) shifted (N[k][LongueurLaby-1]+(10mm,0)));
      endfor;
      fi;
      path CheminSolution;
      CheminSolution=(N[-1][-1] shifted (5mm,5mm))--((center unitsquare scaled 10mm) shifted N[choixligneD][choixcolonneD])
      for k=2 upto indiceChemin-1:
      -- ((center unitsquare scaled 10mm) shifted N[xpart(PileChemin[k])][ypart(PileChemin[k])])
      endfor
      --((center unitsquare scaled 10mm) shifted Arrivee)--N[-2][-2];
      trace CheminSolution dashed evenly withcolor CouleurChemin;
      fi;
      );
      CorpsSolution:=(CorpsSolution scaled \useKV[ClesLabyNb]{Echelle}) rotated \useKV[ClesLabyNb]{Angle};
      trace CorpsSolution;
  \end{mplibcode}
}

\newcommand\TraceLabyNombreDouble[6]{%
  \mplibforcehmode%
  \xdef\PfCNomLabyrinthe{\useKV[ClesLabyNb]{Nom}}%
  \begin{mplibcode}[\PfCNomLabyrinthe]
    input PfCLabyNombre;

    boolean Murs;
    Murs=\useKV[ClesLabyNb]{Murs};
    
    numeric Multiple;
    Multiple=#1;
    % Initialisation du labyrinthe
    InitialisationLabyrinthe(#2,#3);
    % On initialise les paramètres du parcours
    numeric choixligneD,choixligneA,choixcolonneD,choixcolonneA;
    choixligneD=xpart(#4);
    choixcolonneD=ypart(#4);
    choixligneA=xpart(#5);
    choixcolonneA=ypart(#5);%
    pair Depart;
    Depart=N[choixligneD][choixcolonneD];
    pair Arrivee;
    Arrivee=N[choixligneA][choixcolonneA];
    pair Mobile;
    Mobile=Depart;
    RAZPileChemin;
    % Exploration du labyrinthe
    PushChemin((choixligneD,choixcolonneD));
    CaseExploree[choixligneD][choixcolonneD]:=true;
    VoisinDispo(choixligneD,choixcolonneD);
    forever: exitif Mobile=Arrivee;%nb=0;
    nb:=ceiling(uniformdeviate(nbvoisin));
    if nb>0:
      for k=1 upto nbvoisin:
        CaseExploree[xpart(PileVoisin[k])][ypart(PileVoisin[k])]:=true;
      endfor;
      PushChemin((xpart(PileVoisin[nb]),ypart(PileVoisin[nb])));
      Mobile:=N[xpart(PileChemin[indiceChemin])][ypart(PileChemin[indiceChemin])];
      VoisinDispo(xpart(PileChemin[indiceChemin]),ypart(PileChemin[indiceChemin]));
    else:
      PopChemin;
    fi;
    endfor;
    % Affichagefinal
    % on sauvegarde les nombres aléatoires
    numeric NbAffiche[];
    numeric NbSol[];
    % on écrit des nombres au hasard, mais sans être multiple du nombre choisi
    numeric nbaffiche;
    nbaffiche=0;
    NbAffiche[0]=5+ceiling(uniformdeviate(7));
    for k=0 upto LargeurLaby-1:
      for l=0 upto LongueurLaby-1:
      nbaffiche:=nbaffiche+1;
      mathalea:=uniformdeviate(1);
      if mathalea<0.95:
      NbAffiche[nbaffiche]=NbAffiche[nbaffiche-1]+Multiple+1;
      else:
      NbAffiche[nbaffiche]=NbAffiche[nbaffiche-1]*(Multiple-1);
      fi;
      endfor;
    endfor;
    % On crée des multiples du nombre choisi
    NbSol[1]=5+ceiling(uniformdeviate(7));
    for k=2 upto indiceChemin-1:
    mathalea:=uniformdeviate(1);
    if mathalea<0.9:
    NbSol[k]=NbSol[k-1]+Multiple;
    else:
    NbSol[k]=NbSol[k-1]*Multiple;
    fi;
    endfor;
    % On affiche
    picture Corps;
    Corps=image(
      nbaffiche:=0;
      for k=0 upto LargeurLaby-1:
        for l=0 upto LongueurLaby-1:
	  nbaffiche:=nbaffiche+1;
          label(TEX("\num{"&decimal(NbAffiche[nbaffiche])&"}"),M[k][l]);
        endfor;
      endfor;
      remplis ((unitsquare scaled 10mm) shifted N[choixligneD][choixcolonneD]) withcolor \useKV[ClesLabyNb]{Couleur};
      remplis ((unitsquare scaled 10mm) shifted Arrivee) withcolor \useKV[ClesLabyNb]{Couleur};
      for k=2 upto indiceChemin-1:
        remplis ((unitsquare scaled 10mm) shifted N[xpart(PileChemin[k])][ypart(PileChemin[k])]) withcolor white;
        label(TEX("\num{"&decimal(NbSol[k])&"}"),M[xpart(PileChemin[k])][ypart(PileChemin[k])]);
      endfor;
      trace TraceLabyrinthe;
    );
    Corps:=(Corps scaled \useKV[ClesLabyNb]{Echelle}) rotated \useKV[ClesLabyNb]{Angle};
    trace Corps;
  \end{mplibcode}
}

\newcommand\TraceDoubleSolution[6]{%
  \mplibforcehmode%
  \xdef\PfCNomLabyrinthe{\useKV[ClesLabyNb]{Nom}}%
  \begin{mplibcode}[\PfCNomLabyrinthe]
    picture CorpsSolution;
    CorpsSolution=image(
      nbaffiche:=0;
      for k=0 upto #3-1:
        for l=0 upto #2-1:
          nbaffiche:=nbaffiche+1;
          label(TEX("\num{"&decimal(NbAffiche[nbaffiche])&"}"),M[k][l]);
        endfor;
        endfor;
      remplis ((unitsquare scaled 10mm) shifted N[choixligneD][choixcolonneD]) withcolor \useKV[ClesLabyNb]{Couleur};
      remplis ((unitsquare scaled 10mm) shifted Arrivee) withcolor \useKV[ClesLabyNb]{Couleur};
      if \useKV[ClesLabyNb]{Chemin}:
      for k=2 upto indiceChemin-1:
      remplis ((unitsquare scaled 10mm) shifted N[xpart(PileChemin[k])][ypart(PileChemin[k])]) withcolor white;
      endfor;
      trace ((center unitsquare scaled 10mm) shifted N[choixligneD][choixcolonneD])
      for k=2 upto indiceChemin-1:
      -- ((center unitsquare scaled 10mm) shifted N[xpart(PileChemin[k])][ypart(PileChemin[k])])
      endfor
      --((center unitsquare scaled 10mm) shifted Arrivee) dashed evenly withcolor Brown;
      for k=2 upto indiceChemin-1:
      label(TEX("\num{"&decimal(NbSol[k])&"}"),M[xpart(PileChemin[k])][ypart(PileChemin[k])]);
      endfor;
      else:
      for k=2 upto indiceChemin-1:
      remplis ((unitsquare scaled 10mm) shifted N[xpart(PileChemin[k])][ypart(PileChemin[k])]) withcolor \useKV[ClesLabyNb]{CouleurChemin};
      label(TEX("\num{"&decimal(NbSol[k])&"}"),M[xpart(PileChemin[k])][ypart(PileChemin[k])]);
      endfor;
      fi;
      trace TraceLabyrinthe;
      );
      CorpsSolution:=(CorpsSolution scaled \useKV[ClesLabyNb]{Echelle}) rotated \useKV[ClesLabyNb]{Angle};
      trace CorpsSolution;
  \end{mplibcode}
}%