div

div.

/*

Profil
======

div: entier_nat x entier_nat -> entier_nat

Exemples
========

div(4,2) = 2
div(5,2) = 2
div(2,0) = Erreur

division dans les entiers:
a = q*b +r avec 0 <= r < b

5 = 2*2 +1 
5 = 2*1 +3 = 2*2 +1

13/3:
13 = 3 + 10 = 3 + 3 + 3 + 3 + 1
13/3 = 3/3 + 3/3 + 3/3 + 3/3 + 1/3
     = 1 + 1 + 1 + 1 + 0
     = 4

13/3 = 3/3 + 10/3
     = 1   + 3/3 + 7/3
     = 1   +   1 + 3/3 + 4/3
     = 1   +   1 +   1 + 3/3 + 1/3
     = 1   +   1 +   1 +   1 +   0
     = 4

Axiomes:
========

div(n, 0) = Erreur
div(a, succ(b)) = si_alors_sinon_finsi( a < succ(b), 0, 1 + div(a-succ(b), succ(b))

Algorithme recursif
===================

fonction div(a: entier_nat, b: entier_nat): entier_nat
debut
 | si a < b
 |  | alors retourner 0
 |  | sinon retourner 1 + div(a-b, b)
 | finsi
fin

Algorithme iteratif
===================
fonction div(a: entier_nat, b: entier_nat): entier_nat
debut
 | q: entier_nat
 | q := 0
 | tantque a >= b faire
 |  | q := q + 1
 |  | a := a - b
 | fintantque
 | retourner q
fin

*/

#include <stdio.h>
#include "entier_nat.h"

unsigned int divR(unsigned int a, unsigned int b)
{
  if (b == 0) Erreur_Entier_Nat();
  if ( a  < b )
  {
    return 0;
  }
  else
  {
    return 1 + divR(a-b, b);
  }
}

unsigned int divI(unsigned int a, unsigned int b)
{
  unsigned int q = 0;
  if (b == 0) Erreur_Entier_Nat();
  while (a >= b)
  {
    q = q+1;
    a = a-b;
  }
  return q;
}

void testR(unsigned int a, unsigned int b)
{
  printf(" %d / %d = %d (recursif)\n", a, b, divR(a, b));
}

void testI(unsigned int a, unsigned int b)
{
  printf(" %d / %d = %d (iteratif)\n", a, b, divI(a, b));
}

int main()
{
   testR(13,2); 
   testI(13,2); 
   testR(135,24); 
   testI(135,24); 
   testR(3,4); 
   testI(3,4); 
   testI(5,0); 
}