Ppcm avec recursivité

Résolu/Fermé
AMOULA_info Messages postés 3 Statut Membre -  
 lido -
Bonjour,
Bonjour, tout le monde
je veux bien savoir comment calculer le ppcm(a,b) avec la methode recursive en pascal sans l'ajout d'un autre parametre à l'entête de la fonction sachant que je connais la methode itérative avec deux façons:
1ere methode:
function ppcm(a,b: integer):integer;
var i :integer;
begin
i:=1;
if a*b = 0 then ppcm :=0 else
begin
while a*i mod b <> 0 do
i:=i+1;
ppcm:=a*i;
end;
end;
2ème methode:
function ppcm(a,b: integer):integer;
var max,min :integer;
begin
if a*b = 0 then ppcm :=0 else
begin
if a>b then begin max :=a; min:=b ; end
else begin max :=b; min :=a; end;
while max mod min <> 0 do
max := max +(a+b-min);
ppcm:=max;
end;
end;

merci pour votre aide
Configuration: Windows XP
Internet Explorer 6.0

9 réponses

  1. malibous
     
    je vous présente la solution complète de tout le programme qui fait le calcul du ppcm de deux entiers a et b avec la méthode récursive en respectant les contraintes de la saisie de a et b:

    program ppcm_rec;
    uses wincrt;
    var
    a,b:word;

    procedure saisir(var a,b:word);
    begin
    repeat
    writeln('donner a: ');
    readln(a);
    writeln('donner b: ');
    readln(b);
    until ((a>b) and (a>0));
    end;

    function ppcm(a,b:word): longint;

    var
    r:integer;

    begin
    if (b = 0) then
    ppcm := 0
    else if (a mod b = 0) then
    ppcm := a
    else
    begin
    r:=a mod b;
    ppcm:=( a div r)* ppcm(b,r);
    end;
    end;

    begin
    saisir(a,b);
    writeln('le ppcm de ',a,' et ',b,' est ',ppcm(a,b));
    end.
    8
    1. rouna10
       
      function ppcm(a,b:word): longint;

      var
      r:integer;

      begin
      if (b = 0) then
      ppcm := 0
      else if (a mod b = 0) then
      ppcm := a
      else
      ppcm:=( a div (a mod b))* ppcm(a,a mod b);
      end;
      0
    2. rouna10
       
      Si vous exécuter votre programme avec des entier a et b (par exemple a=23 et b=5) vous avez trouvé une fausse résultat.
      Il faut remplacé "ppcm:=( a div r)* ppcm(b,r);" par "ppcm:=( a div r)* ppcm(a,r);"
      0
    3. rouna10
       
      ecrire ce mm algo sans utiliser ni fonction, ni procédure
      0
    4. Sahar
       
      Merci
      0
    5. lido
       
      ce résultat est faux...il faut remplacer ppcm:=( a div r)* ppcm(b,r); par ppcm:= ppcm(b,r)*( a div r); et ce n'est pas la même chose
      0
  2. AMOULA_info Messages postés 3 Statut Membre 9
     
    merci pour vous tous , j'ai trouvé la solution moi même!!!

    function ppcm(a,b:integer) : integer;
    var r : integer;
    begin

    if a mod b = 0 then ppcm := a

    else

    begin
    r:=a mod b;
    ppcm:=( a div r)* ppcm(b,r);
    end;

    end;
    1
    1. dali
       
      salut
      avec tous mes respects à votre point de vue , concernant cette solution (c'est une bonne solution); mais elle manque le test de division par zero :) c-a-d si le 2 eme parametre est egale à zero alors la solution est incorrecte (run time erreur) :) . ok ?
      0
      1. amoula_info > dali
         
        merci pour votre interêt et surtout pour votre politesse mais il suffit d'ajouter un autre test au début pour resoudre ce probleme.
        function ppcm(a,b:integer) : integer;
        var r : integer;
        begin


        if b = 0 then ppcm := 0
        else if a mod b = 0 then ppcm := a

        else

        begin
        r:=a mod b;
        ppcm:=( a div r)* ppcm(b,r);
        end;

        end;
        mon vrai problème etait avec parametres faire le nouvel appel de la fonction.
        et voilà comment j'ai trouvé la solution .
        ppcm(a,b)* pgcd(a,b) = a*b {theoreme mathématique}
        d'où pgcd(a,b ) = (a*b) / ppcm(a,b)
        et puisque pgcd(a,b) = pgcd(b,r) {avec r = a mod b} et pgcd (b,r) = (b*r)/ ppcm(b,r) {} d'aprés le théoreme precdedent}
        on peut conclure que (a*b) / ppcm(a,b) = (b*r) / ppcm(b,r) d'où ppcm(a,b) =( (a*b)/ (b*r))*ppcm(b,r)
        = (a div r )*ppcm(b,r)
        j'espère que j'etais bien claire
        0
      2. 3loulou > amoula_info
         
        MERCI BIEN DE VOTRE AIDE
        0
    2. fenni
       
      Voici une solution optimale :

      Function ppcm (a, b, r : integer) : integer;
      begin
      if (r mod b)=0
      then ppcm := r
      else ppcm := ppcm (a, b, r+a) ;
      end;
      L’appel sera : Writeln ('PPCM = ', ppcm(a,b,a));
      0
  3. Ouhiby
     
    Function ppcmRec1 (a,b,i :integer):integer;
    begin
    if a * b = 0 then
    ppcmR := 0
    else
    if a * i mod b = 0 then
    ppcmR := a * i
    else
    ppcmR := ppcmR(a,b,i+1);
    End;

    Appeler X :=ppcmRec1(a,b,1)
    1
  4. amoula_info
     
    salut Ouhiby
    pour la fonction recursive ppcmrec2 je pense ( et je l'ai tester d'ailleur) qu'elle est fausse.
    1
    1. Ouhiby
       
      Ah oui, j'ai pas pu la corrigé !!!
      qu'est ce que vous en dites ?
      0
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. lkj
     
    j' ai trouver plusieur solution
    mais la quel la plus juste
    svp
    trés urgent!!!!!!!!!!!
    1
    1. rouna10
       
      function ppcm(a,b:word): longint;

      var
      r:integer;

      begin
      if (b = 0) then
      ppcm := 0
      else if (a mod b = 0) then
      ppcm := a
      else
      ppcm:=( a div (a mod b))* ppcm(a,a mod b);
      end;

      { c'est la plus just}
      0
  7. rihab chebbah
     
    salut, SVP j ve un programme qui permet de saisir et d'afficher le ppcm de deux nombre ( le ppcm doit etre sous la formes des entier avec ses exposon!!) compris!! svp i need it
    1
  8. Malebete
     
    Etonnant le nombre d'algorithmes proposés qui supposent a >= b ;)

    Testez-les avec ppcm(1, 53), voire ppcm(0, 22) ;))
    1
  9. dorsaf007
     
    Function ppcm (a,b:integer):integer;
    begin
    if a=b then ppcm:=a
    else if a>b
    then ppcm:=(ppcm(a-b,b)*a) div (a-b)
    else ppcm:=(ppcm(a,b-a)*b) div (b-a);
    end;
    1
  10. Ouhiby
     
    Pardon j'ai commis des erreurs d'appel :)
    Voici la solution complète InChaallah

    Program testppcm ;
    uses wincrt;

    function ppcm1(a,b: integer):integer;
    var i :integer;
    begin
    i:=1;
    if a*b = 0 then
    ppcm1 :=0
    else
    begin
    while a*i mod b <> 0 do
    i:=i+1;
    ppcm1:=a*i;
    end;
    end;

    Function ppcmRec1 (a,b,i :integer):integer;
    begin
    if a * b = 0 then
    ppcmRec1 := 0
    else
    if a * i mod b = 0 then
    ppcmRec1 := a * i
    else
    ppcmRec1 := ppcmRec1(a,b,i+1);
    End;

    function ppcm2(a,b: integer):integer;
    var max,min :integer;
    begin
    if a*b = 0 then
    ppcm2 :=0
    else
    begin
    if a>b then
    begin
    max :=a;
    min:=b ;
    end
    else
    begin
    max :=b;
    min :=a;
    end;
    while max mod min <> 0 do
    max := max +(a+b-min);
    ppcm2:=max;
    end;
    end;

    function ppcmRec2(a,b: integer):integer;
    Var
    max,min : integer;
    Begin

    if a*b = 0 then
    ppcmRec2 :=0
    else
    begin
    if a>b then
    begin
    max :=a;
    min:=b ;
    end
    else
    begin
    max :=b;
    min :=a;
    end;
    if max mod min = 0 then
    ppcmRec2:=max
    else
    ppcmRec2 := ppcmRec2(max +(a+b-min),b);

    end;

    End;

    Begin
    {exemple d'aapel}
    Writeln(ppcm1(15,3));
    Writeln(ppcmRec1(3,15,1));
    Writeln(ppcm2(4,12));
    Writeln(ppcmRec2(23,42));

    {
    Résultat :
    15
    15
    12
    84
    }
    End.
    0