Bac Pratique 01062019 Algo Corrige4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

C:\Users\user\Desktop\examen 2109.

pas

Program CalcPPCM;
Uses WinCrt;
Type
tab=Array[1..100] of Integer;
enreg=Record
a,b:Integer;
ppcm:LongInt;
end;
fiche=file of enreg;
Var
n:Integer;
fe:fiche;

Procedure saisie(var n:Integer);


Begin
Repeat
write('n:=');Readln(n);
Until n in [2..100];
End;

Procedure Facteurs_premiers(K:Integer;Var T:tab; Var x:Integer);


Var
i:Integer;
Begin
i:=2; x:=0;
Repeat
if K mod i =0 Then
Begin
inc(x);
T[x]:=i;
K:= K DIV i;
End
Else
inc(i);
Until K=1;
end;

Function existence(elt:Integer; T:tab; x:Integer):Boolean;


Var
j:Integer;
Begin
j:=0;
Repeat
inc(j);
Until (j>x) Or (elt=T[j]);
existence:=(j<=x);
end;

Procedure Distincts_elts(T:tab;x:Integer;Var V:tab;var y:Integer);


Var
i:Integer;
Begin
V[1]:=T[1]; y:=1;
For i:= 2 to x Do
if Not existence(T[i],V,y) Then
Begin
inc(y);
V[y]:=T[i];
end;
End;

Devoirs et examens sur : www.Kiteb.net


Function nbre_occurrence(elt:Integer;T:tab;x:Integer):Integer;
Var
nbre,i:Integer;
Begin
nbre:=0;
For i:=1 to X Do
If T[i]=elt Then
inc(nbre);
nbre_occurrence:=nbre;
End;

Function Puissance(x,y:Integer):LongInt;
Var
p:LongInt;
i:Integer;

Begin
p:=1;
for i:=1 To y Do
p:=p*x;
Puissance:=p;
end;

01-06-2019 - 18:46:16 - Page 1/2


C:\Users\user\Desktop\examen 2109.pas

Function calcul_ppcm(a,b:Integer):Integer;
Var
TA,TB,DA,DB:tab;
i,m,p,nb_a,nb_b:Integer;
resultat:longint;

Begin
Facteurs_premiers(a,TA,m);
Facteurs_premiers(b,TB,p);
Distincts_elts(TA,m,DA,nb_a);
Distincts_elts(TB,p,DB,nb_b);

resultat:=1;

For i:=1 to nb_a Do


Begin
if existence(DA[i],DB,nb_b) Then
if nbre_occurrence(DA[i],TA,m) > nbre_occurrence(DA[i],TB,p) Then
resultat:=resultat * puissance( DA[i], nbre_occurrence(DA[i],TA,m) )
Else
resultat:=resultat * puissance( DA[i], nbre_occurrence(DA[i],TB,p) )
Else
resultat:=resultat * puissance(DA[i], nbre_occurrence(DA[i],TA,m));
End;

For i:=1 to nb_b Do


Begin
if Not existence(DB[i],DA,nb_a) Then
resultat:=resultat * puissance(DB[i], nbre_occurrence(DB[i],TB,p));
End;

calcul_ppcm:=resultat;
End;

Procedure remplissage(n:Integer; Var fe:fiche);


Var
i,a,b:Integer;
e:enreg;
Begin
rewrite(fe);
For i:= 1 to n Do
Begin

Repeat
write('Donner a n°',i,':=');Readln(a);
Until (a>0) and (a<=1000);

e.a:=a;

Repeat
write('Donner b n°',i,':=');Readln(b);
Until (b>0) and (b<=1000);

e.b:=b;

e.ppcm:=calcul_ppcm(a,b);

Write(fe,e);
End;
close(fe);

Devoirs et examens sur : www.Kiteb.net


End;

Procedure affichage(var fe:fiche);


Var
e:enreg;
Begin
reset(fe);
While not eof(fe) Do
Begin
read(fe,e);
writeln('PPCM( ',e.a,' , ',e.b,' )=',e.ppcm);
End;
close(fe);
End;

Begin
saisie(n);
Assign(fe,'c:\F_PPCM.dat');
remplissage(n,fe);
affichage(fe);
End.

01-06-2019 - 18:46:16 - Page 2/2

You might also like