Sunteți pe pagina 1din 253

Solutions de TD 1

Prof. Abdelmajid Dargham


Faculté des Sciences, Oujda
Master Spécialisé Ingénierie Informatique

Université Mohamed Premier

Septembre, 2012
Exercice 1

Considérons l’algorithme suivant :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

Considérons l’algorithme suivant :


Fonction F(n : Entier) : Entier;
Variables
x, y : Entier;
Début
x := n;
y := n;
Tant Que (y 6= 0) Faire
x := x + 2;
y := y − 1;
FinTQ
Retourner(x);
Fin
Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes
Exercice 1

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

1 Calculons F (0), F (1) et F (2) :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

1 Calculons F (0), F (1) et F (2) :


F (0) = 0 : x et y sont initialisées à 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

1 Calculons F (0), F (1) et F (2) :


F (0) = 0 : x et y sont initialisées à 0.
Comme y = 0, la boucle ne sera pas exécutée.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

1 Calculons F (0), F (1) et F (2) :


F (0) = 0 : x et y sont initialisées à 0.
Comme y = 0, la boucle ne sera pas exécutée.
On sort alors avec x = 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

1 Calculons F (0), F (1) et F (2) :


F (0) = 0 : x et y sont initialisées à 0.
Comme y = 0, la boucle ne sera pas exécutée.
On sort alors avec x = 0.
F (1) = 3 : x et y sont initialisées à 1.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

1 Calculons F (0), F (1) et F (2) :


F (0) = 0 : x et y sont initialisées à 0.
Comme y = 0, la boucle ne sera pas exécutée.
On sort alors avec x = 0.
F (1) = 3 : x et y sont initialisées à 1.
Comme y = 1, on exécute la boucle : x = 2 + 1 = 3 et
y = 1 − 1 = 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

1 Calculons F (0), F (1) et F (2) :


F (0) = 0 : x et y sont initialisées à 0.
Comme y = 0, la boucle ne sera pas exécutée.
On sort alors avec x = 0.
F (1) = 3 : x et y sont initialisées à 1.
Comme y = 1, on exécute la boucle : x = 2 + 1 = 3 et
y = 1 − 1 = 0.
Maintenant, y = 0, on quitte la boucle et l’on retourne
x = 3.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

1 Calculons F (0), F (1) et F (2) :


F (0) = 0 : x et y sont initialisées à 0.
Comme y = 0, la boucle ne sera pas exécutée.
On sort alors avec x = 0.
F (1) = 3 : x et y sont initialisées à 1.
Comme y = 1, on exécute la boucle : x = 2 + 1 = 3 et
y = 1 − 1 = 0.
Maintenant, y = 0, on quitte la boucle et l’on retourne
x = 3.
F (2) = 6 : x et y sont initialisées à 2.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

1 Calculons F (0), F (1) et F (2) :


F (0) = 0 : x et y sont initialisées à 0.
Comme y = 0, la boucle ne sera pas exécutée.
On sort alors avec x = 0.
F (1) = 3 : x et y sont initialisées à 1.
Comme y = 1, on exécute la boucle : x = 2 + 1 = 3 et
y = 1 − 1 = 0.
Maintenant, y = 0, on quitte la boucle et l’on retourne
x = 3.
F (2) = 6 : x et y sont initialisées à 2.
Comme y = 2, on exécute la boucle : x = 2 + 2 = 4 et
y = 2 − 1 = 1.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

1 Calculons F (0), F (1) et F (2) :


F (0) = 0 : x et y sont initialisées à 0.
Comme y = 0, la boucle ne sera pas exécutée.
On sort alors avec x = 0.
F (1) = 3 : x et y sont initialisées à 1.
Comme y = 1, on exécute la boucle : x = 2 + 1 = 3 et
y = 1 − 1 = 0.
Maintenant, y = 0, on quitte la boucle et l’on retourne
x = 3.
F (2) = 6 : x et y sont initialisées à 2.
Comme y = 2, on exécute la boucle : x = 2 + 2 = 4 et
y = 2 − 1 = 1.
Comme y = 1, on exécute de nouveau la boucle :
x = 4 + 2 = 6 et y = 1 − 1 = 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

1 Calculons F (0), F (1) et F (2) :


F (0) = 0 : x et y sont initialisées à 0.
Comme y = 0, la boucle ne sera pas exécutée.
On sort alors avec x = 0.
F (1) = 3 : x et y sont initialisées à 1.
Comme y = 1, on exécute la boucle : x = 2 + 1 = 3 et
y = 1 − 1 = 0.
Maintenant, y = 0, on quitte la boucle et l’on retourne
x = 3.
F (2) = 6 : x et y sont initialisées à 2.
Comme y = 2, on exécute la boucle : x = 2 + 2 = 4 et
y = 2 − 1 = 1.
Comme y = 1, on exécute de nouveau la boucle :
x = 4 + 2 = 6 et y = 1 − 1 = 0.
y = 0, donc on sort de la boucle et l’on retourne la
valeur x = 6.
Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes
Exercice 1

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

2 Déterminons ce que calcule la fonction F (n), pour tout


entier n ≥ 0 :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

2 Déterminons ce que calcule la fonction F (n), pour tout


entier n ≥ 0 :
Avant la boucle, x = n et y = n.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

2 Déterminons ce que calcule la fonction F (n), pour tout


entier n ≥ 0 :
Avant la boucle, x = n et y = n.
La boucle décrémente y de 1 et incrémente x de 2.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

2 Déterminons ce que calcule la fonction F (n), pour tout


entier n ≥ 0 :
Avant la boucle, x = n et y = n.
La boucle décrémente y de 1 et incrémente x de 2.
Il y aura alors n itérations.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

2 Déterminons ce que calcule la fonction F (n), pour tout


entier n ≥ 0 :
Avant la boucle, x = n et y = n.
La boucle décrémente y de 1 et incrémente x de 2.
Il y aura alors n itérations.
La valeur finale de x est donc : n + 2n = 3n.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

2 Déterminons ce que calcule la fonction F (n), pour tout


entier n ≥ 0 :
Avant la boucle, x = n et y = n.
La boucle décrémente y de 1 et incrémente x de 2.
Il y aura alors n itérations.
La valeur finale de x est donc : n + 2n = 3n.
Par suite, F (n) = 3n, ∀n ≥ 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

3 Prouvons l’invariant de boucle : xj + 2yj = 3n, pour tout


entier j ≥ 0 :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

3 Prouvons l’invariant de boucle : xj + 2yj = 3n, pour tout


entier j ≥ 0 :
Commençons par les faits :
x0 = n et y0 = n.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

3 Prouvons l’invariant de boucle : xj + 2yj = 3n, pour tout


entier j ≥ 0 :
Commençons par les faits :
x0 = n et y0 = n.
xj+1 = xj + 2, ∀j ≥ 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

3 Prouvons l’invariant de boucle : xj + 2yj = 3n, pour tout


entier j ≥ 0 :
Commençons par les faits :
x0 = n et y0 = n.
xj+1 = xj + 2, ∀j ≥ 0.
yj+1 = yj − 1, ∀j ≥ 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

3 Prouvons l’invariant de boucle : xj + 2yj = 3n, pour tout


entier j ≥ 0 :
Commençons par les faits :
x0 = n et y0 = n.
xj+1 = xj + 2, ∀j ≥ 0.
yj+1 = yj − 1, ∀j ≥ 0.
Preuve de l’invariant de boucle (Par récurrence sur j).

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

3 Prouvons l’invariant de boucle : xj + 2yj = 3n, pour tout


entier j ≥ 0 :
Commençons par les faits :
x0 = n et y0 = n.
xj+1 = xj + 2, ∀j ≥ 0.
yj+1 = yj − 1, ∀j ≥ 0.
Preuve de l’invariant de boucle (Par récurrence sur j).
Base : pour j = 0, on a x0 + 2y0 = n + 2n = 3n. La
propriété est vérifiée pour j = 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

3 Prouvons l’invariant de boucle : xj + 2yj = 3n, pour tout


entier j ≥ 0 :
Commençons par les faits :
x0 = n et y0 = n.
xj+1 = xj + 2, ∀j ≥ 0.
yj+1 = yj − 1, ∀j ≥ 0.
Preuve de l’invariant de boucle (Par récurrence sur j).
Base : pour j = 0, on a x0 + 2y0 = n + 2n = 3n. La
propriété est vérifiée pour j = 0.
Induction : soit j ≥ 0 et supposons que xj + 2yj = 3n.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

3 Prouvons l’invariant de boucle : xj + 2yj = 3n, pour tout


entier j ≥ 0 :
Commençons par les faits :
x0 = n et y0 = n.
xj+1 = xj + 2, ∀j ≥ 0.
yj+1 = yj − 1, ∀j ≥ 0.
Preuve de l’invariant de boucle (Par récurrence sur j).
Base : pour j = 0, on a x0 + 2y0 = n + 2n = 3n. La
propriété est vérifiée pour j = 0.
Induction : soit j ≥ 0 et supposons que xj + 2yj = 3n.
Montrons que xj+1 + 2yj+1 = 3n.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

3 Prouvons l’invariant de boucle : xj + 2yj = 3n, pour tout


entier j ≥ 0 :
Commençons par les faits :
x0 = n et y0 = n.
xj+1 = xj + 2, ∀j ≥ 0.
yj+1 = yj − 1, ∀j ≥ 0.
Preuve de l’invariant de boucle (Par récurrence sur j).
Base : pour j = 0, on a x0 + 2y0 = n + 2n = 3n. La
propriété est vérifiée pour j = 0.
Induction : soit j ≥ 0 et supposons que xj + 2yj = 3n.
Montrons que xj+1 + 2yj+1 = 3n. On a :
xj+1 + 2yj+1 = xj + 2 + 2(yj − 1) = xj + 2yj = 3n.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

4 Donnons alors la preuve de l’algorithme F (n) :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

4 Donnons alors la preuve de l’algorithme F (n) :


L’algorithme F (n) se termine, car il effectue n itérations
et chaque itération se termine.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

4 Donnons alors la preuve de l’algorithme F (n) :


L’algorithme F (n) se termine, car il effectue n itérations
et chaque itération se termine.
Exploitation de l’invariant de boucle :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

4 Donnons alors la preuve de l’algorithme F (n) :


L’algorithme F (n) se termine, car il effectue n itérations
et chaque itération se termine.
Exploitation de l’invariant de boucle :
(yj )j≥0 est une suite arithmétique de premier terme
y0 = n et de raison r = −1.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

4 Donnons alors la preuve de l’algorithme F (n) :


L’algorithme F (n) se termine, car il effectue n itérations
et chaque itération se termine.
Exploitation de l’invariant de boucle :
(yj )j≥0 est une suite arithmétique de premier terme
y0 = n et de raison r = −1.
Donc, yj = y0 + r (j − 0) = n − j, ∀j ≥ 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

4 Donnons alors la preuve de l’algorithme F (n) :


L’algorithme F (n) se termine, car il effectue n itérations
et chaque itération se termine.
Exploitation de l’invariant de boucle :
(yj )j≥0 est une suite arithmétique de premier terme
y0 = n et de raison r = −1.
Donc, yj = y0 + r (j − 0) = n − j, ∀j ≥ 0.
On en déduit que xj = 3n − 2yj = n + 2j.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

4 Donnons alors la preuve de l’algorithme F (n) :


L’algorithme F (n) se termine, car il effectue n itérations
et chaque itération se termine.
Exploitation de l’invariant de boucle :
(yj )j≥0 est une suite arithmétique de premier terme
y0 = n et de raison r = −1.
Donc, yj = y0 + r (j − 0) = n − j, ∀j ≥ 0.
On en déduit que xj = 3n − 2yj = n + 2j.
Comme il y a n itérations, la dernière valeur de j est n,
et par suite, xn = n + 2n = 3n.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 1

4 Donnons alors la preuve de l’algorithme F (n) :


L’algorithme F (n) se termine, car il effectue n itérations
et chaque itération se termine.
Exploitation de l’invariant de boucle :
(yj )j≥0 est une suite arithmétique de premier terme
y0 = n et de raison r = −1.
Donc, yj = y0 + r (j − 0) = n − j, ∀j ≥ 0.
On en déduit que xj = 3n − 2yj = n + 2j.
Comme il y a n itérations, la dernière valeur de j est n,
et par suite, xn = n + 2n = 3n.
Or, F (n) = xn , donc F (n) = 3n et l’algorithme est
correct.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On considère l’algorithme suivant :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On considère l’algorithme suivant :


Fonction F(n : Entier) : Entier;
Début
Si (n ≤ 1) Alors
Retourner(n);
Sinon
Retourner 5 × F (n − 1) − 6 × F (n − 2);
FinSi
Fin

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

1 Prouvons que l’algorithme F (n) calcule 3n − 2n , pour tout


n≥0:

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

1 Prouvons que l’algorithme F (n) calcule 3n − 2n , pour tout


n≥0:
La preuve sera par récurrence (forte) sur n.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

1 Prouvons que l’algorithme F (n) calcule 3n − 2n , pour tout


n≥0:
La preuve sera par récurrence (forte) sur n.
Base :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

1 Prouvons que l’algorithme F (n) calcule 3n − 2n , pour tout


n≥0:
La preuve sera par récurrence (forte) sur n.
Base :
Si n = 0, l’algorithme F se termine et retourne la valeur
0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

1 Prouvons que l’algorithme F (n) calcule 3n − 2n , pour tout


n≥0:
La preuve sera par récurrence (forte) sur n.
Base :
Si n = 0, l’algorithme F se termine et retourne la valeur
0. Or, 30 − 20 = 1 − 1 = 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

1 Prouvons que l’algorithme F (n) calcule 3n − 2n , pour tout


n≥0:
La preuve sera par récurrence (forte) sur n.
Base :
Si n = 0, l’algorithme F se termine et retourne la valeur
0. Or, 30 − 20 = 1 − 1 = 0.
De même, si n = 1, l’algorithme F se termine et
retourne la valeur 1.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

1 Prouvons que l’algorithme F (n) calcule 3n − 2n , pour tout


n≥0:
La preuve sera par récurrence (forte) sur n.
Base :
Si n = 0, l’algorithme F se termine et retourne la valeur
0. Or, 30 − 20 = 1 − 1 = 0.
De même, si n = 1, l’algorithme F se termine et
retourne la valeur 1. Or, 31 − 21 = 3 − 2 = 1.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

1 Prouvons que l’algorithme F (n) calcule 3n − 2n , pour tout


n≥0:
La preuve sera par récurrence (forte) sur n.
Base :
Si n = 0, l’algorithme F se termine et retourne la valeur
0. Or, 30 − 20 = 1 − 1 = 0.
De même, si n = 1, l’algorithme F se termine et
retourne la valeur 1. Or, 31 − 21 = 3 − 2 = 1.
Donc, si n = 0 ou n = 1, l’algorithme F se termine et il
est correct.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction :
Hypothèse de récurrence : soit n ≥ 2 et supposons
que l’algorithme F se termine et retourne la valeur
3k − 2k pour tout entier k tel que 0 ≤ k < n.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction :
Hypothèse de récurrence : soit n ≥ 2 et supposons
que l’algorithme F se termine et retourne la valeur
3k − 2k pour tout entier k tel que 0 ≤ k < n.
Démontrons maintenant que l’algorithme F (n) se
termine et retourne la valeur 3n − 2n .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction :
Hypothèse de récurrence : soit n ≥ 2 et supposons
que l’algorithme F se termine et retourne la valeur
3k − 2k pour tout entier k tel que 0 ≤ k < n.
Démontrons maintenant que l’algorithme F (n) se
termine et retourne la valeur 3n − 2n .
Puisque n ≥ 2, alors l’algorithme F (n) va engendrer
deux appels récursifs : F (n − 1) et F (n − 2).

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction :
Hypothèse de récurrence : soit n ≥ 2 et supposons
que l’algorithme F se termine et retourne la valeur
3k − 2k pour tout entier k tel que 0 ≤ k < n.
Démontrons maintenant que l’algorithme F (n) se
termine et retourne la valeur 3n − 2n .
Puisque n ≥ 2, alors l’algorithme F (n) va engendrer
deux appels récursifs : F (n − 1) et F (n − 2).
D’après l’hypothèse de récurrence, ces deux appels se
terminent et retournent respectivement 3n−1 − 2n−1 et
3n−2 − 2n−2 .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction :
Hypothèse de récurrence : soit n ≥ 2 et supposons
que l’algorithme F se termine et retourne la valeur
3k − 2k pour tout entier k tel que 0 ≤ k < n.
Démontrons maintenant que l’algorithme F (n) se
termine et retourne la valeur 3n − 2n .
Puisque n ≥ 2, alors l’algorithme F (n) va engendrer
deux appels récursifs : F (n − 1) et F (n − 2).
D’après l’hypothèse de récurrence, ces deux appels se
terminent et retournent respectivement 3n−1 − 2n−1 et
3n−2 − 2n−2 .
Donc, l’algorithme F (n) se termine et retourne la valeur
5 × (3n−1 − 2n−1 ) − 6 × (3n−2 − 2n−2 ).

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Or, 5 × (3n−1 − 2n−1 ) − ×6(3n−2 − 2n−2 ) =

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Or, 5 × (3n−1 − 2n−1 ) − ×6(3n−2 − 2n−2 ) =


(2 + 3) × (3n−1 − 2n−1 ) − (2 × 3) × (3n−2 − 2n−2 ) =

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Or, 5 × (3n−1 − 2n−1 ) − ×6(3n−2 − 2n−2 ) =


(2 + 3) × (3n−1 − 2n−1 ) − (2 × 3) × (3n−2 − 2n−2 ) =
2 × 3n−1 + 3n − 2n − 3 × 2n−1 − 2 × 3n−1 + 3 × 2n−1 =

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Or, 5 × (3n−1 − 2n−1 ) − ×6(3n−2 − 2n−2 ) =


(2 + 3) × (3n−1 − 2n−1 ) − (2 × 3) × (3n−2 − 2n−2 ) =
2 × 3n−1 + 3n − 2n − 3 × 2n−1 − 2 × 3n−1 + 3 × 2n−1 =
3n − 2n .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Or, 5 × (3n−1 − 2n−1 ) − ×6(3n−2 − 2n−2 ) =


(2 + 3) × (3n−1 − 2n−1 ) − (2 × 3) × (3n−2 − 2n−2 ) =
2 × 3n−1 + 3n − 2n − 3 × 2n−1 − 2 × 3n−1 + 3 × 2n−1 =
3n − 2n .
Par suite, l’algorithme est correct.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :
Fonction G(n : Entier) : Entier;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :
Fonction G(n : Entier) : Entier;
Variables

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :
Fonction G(n : Entier) : Entier;
Variables
t : liste d’entiers;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :
Fonction G(n : Entier) : Entier;
Variables
t : liste d’entiers; i : Entier;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :
Fonction G(n : Entier) : Entier;
Variables
t : liste d’entiers; i : Entier;
Début

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :
Fonction G(n : Entier) : Entier;
Variables
t : liste d’entiers; i : Entier;
Début
t[0] := 0;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :
Fonction G(n : Entier) : Entier;
Variables
t : liste d’entiers; i : Entier;
Début
t[0] := 0; t[1] := 1;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :
Fonction G(n : Entier) : Entier;
Variables
t : liste d’entiers; i : Entier;
Début
t[0] := 0; t[1] := 1;
i := 2;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :
Fonction G(n : Entier) : Entier;
Variables
t : liste d’entiers; i : Entier;
Début
t[0] := 0; t[1] := 1;
i := 2;
Tant Que (i ≤ n) Faire

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :
Fonction G(n : Entier) : Entier;
Variables
t : liste d’entiers; i : Entier;
Début
t[0] := 0; t[1] := 1;
i := 2;
Tant Que (i ≤ n) Faire
t[i] := 5 × t[i − 1] − 6 × t[i − 2];

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :
Fonction G(n : Entier) : Entier;
Variables
t : liste d’entiers; i : Entier;
Début
t[0] := 0; t[1] := 1;
i := 2;
Tant Que (i ≤ n) Faire
t[i] := 5 × t[i − 1] − 6 × t[i − 2];
i := i + 1;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :
Fonction G(n : Entier) : Entier;
Variables
t : liste d’entiers; i : Entier;
Début
t[0] := 0; t[1] := 1;
i := 2;
Tant Que (i ≤ n) Faire
t[i] := 5 × t[i − 1] − 6 × t[i − 2];
i := i + 1;
FinTQ

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :
Fonction G(n : Entier) : Entier;
Variables
t : liste d’entiers; i : Entier;
Début
t[0] := 0; t[1] := 1;
i := 2;
Tant Que (i ≤ n) Faire
t[i] := 5 × t[i − 1] − 6 × t[i − 2];
i := i + 1;
FinTQ
Retourner (t[n]);
Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes
Exercice 2

2 En utilisant un tableau, écrivons un algorithme itératif


G (n) qui soit équivalent à F (n) :
Fonction G(n : Entier) : Entier;
Variables
t : liste d’entiers; i : Entier;
Début
t[0] := 0; t[1] := 1;
i := 2;
Tant Que (i ≤ n) Faire
t[i] := 5 × t[i − 1] − 6 × t[i − 2];
i := i + 1;
FinTQ
Retourner (t[n]);
Fin
Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes
Exercice 2

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

3 Donnons la preuve de l’algorithme G (n) :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

3 Donnons la preuve de l’algorithme G (n) :


Si n = 0, l’algorithme G se termine et retourne
t[0] = 0 = 30 − 20 .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

3 Donnons la preuve de l’algorithme G (n) :


Si n = 0, l’algorithme G se termine et retourne
t[0] = 0 = 30 − 20 .
Si n = 1, l’algorithme G se termine et retourne
t[1] = 1 = 31 − 21 .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

3 Donnons la preuve de l’algorithme G (n) :


Si n = 0, l’algorithme G se termine et retourne
t[0] = 0 = 30 − 20 .
Si n = 1, l’algorithme G se termine et retourne
t[1] = 1 = 31 − 21 .
Donc, si n = 0 ou n = 1, l’algorithme G se termine et il
est correct.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

3 Donnons la preuve de l’algorithme G (n) :


Si n = 0, l’algorithme G se termine et retourne
t[0] = 0 = 30 − 20 .
Si n = 1, l’algorithme G se termine et retourne
t[1] = 1 = 31 − 21 .
Donc, si n = 0 ou n = 1, l’algorithme G se termine et il
est correct.
Soit n ≥ 2. La boucle Tant Que est effectuée
n − 2 + 1 = n − 1 fois, et son corps se termine.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

3 Donnons la preuve de l’algorithme G (n) :


Si n = 0, l’algorithme G se termine et retourne
t[0] = 0 = 30 − 20 .
Si n = 1, l’algorithme G se termine et retourne
t[1] = 1 = 31 − 21 .
Donc, si n = 0 ou n = 1, l’algorithme G se termine et il
est correct.
Soit n ≥ 2. La boucle Tant Que est effectuée
n − 2 + 1 = n − 1 fois, et son corps se termine.
Par suite, l’algorithme se termine.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

3 Donnons la preuve de l’algorithme G (n) :


Si n = 0, l’algorithme G se termine et retourne
t[0] = 0 = 30 − 20 .
Si n = 1, l’algorithme G se termine et retourne
t[1] = 1 = 31 − 21 .
Donc, si n = 0 ou n = 1, l’algorithme G se termine et il
est correct.
Soit n ≥ 2. La boucle Tant Que est effectuée
n − 2 + 1 = n − 1 fois, et son corps se termine.
Par suite, l’algorithme se termine.
Les faits:

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

3 Donnons la preuve de l’algorithme G (n) :


Si n = 0, l’algorithme G se termine et retourne
t[0] = 0 = 30 − 20 .
Si n = 1, l’algorithme G se termine et retourne
t[1] = 1 = 31 − 21 .
Donc, si n = 0 ou n = 1, l’algorithme G se termine et il
est correct.
Soit n ≥ 2. La boucle Tant Que est effectuée
n − 2 + 1 = n − 1 fois, et son corps se termine.
Par suite, l’algorithme se termine.
Les faits:
i0 = 2

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

3 Donnons la preuve de l’algorithme G (n) :


Si n = 0, l’algorithme G se termine et retourne
t[0] = 0 = 30 − 20 .
Si n = 1, l’algorithme G se termine et retourne
t[1] = 1 = 31 − 21 .
Donc, si n = 0 ou n = 1, l’algorithme G se termine et il
est correct.
Soit n ≥ 2. La boucle Tant Que est effectuée
n − 2 + 1 = n − 1 fois, et son corps se termine.
Par suite, l’algorithme se termine.
Les faits:
i0 = 2
t[ij−1 ] = 5 × t[ij−1 − 1] − 6 × t[ij−1 − 2], ∀j ≥ 1

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

3 Donnons la preuve de l’algorithme G (n) :


Si n = 0, l’algorithme G se termine et retourne
t[0] = 0 = 30 − 20 .
Si n = 1, l’algorithme G se termine et retourne
t[1] = 1 = 31 − 21 .
Donc, si n = 0 ou n = 1, l’algorithme G se termine et il
est correct.
Soit n ≥ 2. La boucle Tant Que est effectuée
n − 2 + 1 = n − 1 fois, et son corps se termine.
Par suite, l’algorithme se termine.
Les faits:
i0 = 2
t[ij−1 ] = 5 × t[ij−1 − 1] − 6 × t[ij−1 − 2], ∀j ≥ 1
ij = ij−1 + 1, ∀j ≥ 1
Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes
Exercice 2

Explication des faits :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Explication des faits :


Considérons la j eme itération, avec j ≥ 1.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Explication des faits :


Considérons la j eme itération, avec j ≥ 1.
La variable i est impliquée dans deux instructions :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Explication des faits :


Considérons la j eme itération, avec j ≥ 1.
La variable i est impliquée dans deux instructions :
La première est : t[i] := 5 × t[i − 1] − 6 × t[i − 2].

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Explication des faits :


Considérons la j eme itération, avec j ≥ 1.
La variable i est impliquée dans deux instructions :
La première est : t[i] := 5 × t[i − 1] − 6 × t[i − 2]. Dans
cette affectation, on doit utiliser la valeur de i avant la
j eme itération.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Explication des faits :


Considérons la j eme itération, avec j ≥ 1.
La variable i est impliquée dans deux instructions :
La première est : t[i] := 5 × t[i − 1] − 6 × t[i − 2]. Dans
cette affectation, on doit utiliser la valeur de i avant la
j eme itération. C’est donc ij−1 .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Explication des faits :


Considérons la j eme itération, avec j ≥ 1.
La variable i est impliquée dans deux instructions :
La première est : t[i] := 5 × t[i − 1] − 6 × t[i − 2]. Dans
cette affectation, on doit utiliser la valeur de i avant la
j eme itération. C’est donc ij−1 .
La deuxième est : i := i + 1.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Explication des faits :


Considérons la j eme itération, avec j ≥ 1.
La variable i est impliquée dans deux instructions :
La première est : t[i] := 5 × t[i − 1] − 6 × t[i − 2]. Dans
cette affectation, on doit utiliser la valeur de i avant la
j eme itération. C’est donc ij−1 .
La deuxième est : i := i + 1. Cette affectation va
changer la valeur courante de i.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Explication des faits :


Considérons la j eme itération, avec j ≥ 1.
La variable i est impliquée dans deux instructions :
La première est : t[i] := 5 × t[i − 1] − 6 × t[i − 2]. Dans
cette affectation, on doit utiliser la valeur de i avant la
j eme itération. C’est donc ij−1 .
La deuxième est : i := i + 1. Cette affectation va
changer la valeur courante de i. La valeur de i à gauche
est ij et la valeur de i à droite est ij−1 .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Explication des faits :


Considérons la j eme itération, avec j ≥ 1.
La variable i est impliquée dans deux instructions :
La première est : t[i] := 5 × t[i − 1] − 6 × t[i − 2]. Dans
cette affectation, on doit utiliser la valeur de i avant la
j eme itération. C’est donc ij−1 .
La deuxième est : i := i + 1. Cette affectation va
changer la valeur courante de i. La valeur de i à gauche
est ij et la valeur de i à droite est ij−1 .
Par conséquent :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Explication des faits :


Considérons la j eme itération, avec j ≥ 1.
La variable i est impliquée dans deux instructions :
La première est : t[i] := 5 × t[i − 1] − 6 × t[i − 2]. Dans
cette affectation, on doit utiliser la valeur de i avant la
j eme itération. C’est donc ij−1 .
La deuxième est : i := i + 1. Cette affectation va
changer la valeur courante de i. La valeur de i à gauche
est ij et la valeur de i à droite est ij−1 .
Par conséquent :
t[ij−1 ] = 5 × t[ij−1 − 1] − 6 × t[ij−1 − 2], ∀j ≥ 1

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Explication des faits :


Considérons la j eme itération, avec j ≥ 1.
La variable i est impliquée dans deux instructions :
La première est : t[i] := 5 × t[i − 1] − 6 × t[i − 2]. Dans
cette affectation, on doit utiliser la valeur de i avant la
j eme itération. C’est donc ij−1 .
La deuxième est : i := i + 1. Cette affectation va
changer la valeur courante de i. La valeur de i à gauche
est ij et la valeur de i à droite est ij−1 .
Par conséquent :
t[ij−1 ] = 5 × t[ij−1 − 1] − 6 × t[ij−1 − 2], ∀j ≥ 1
ij = ij−1 + 1, ∀j ≥ 1

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :
ij = i0 + 1 × (j − 0) = j + 2, ∀j ≥ 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :
ij = i0 + 1 × (j − 0) = j + 2, ∀j ≥ 0.
Alors : ij−1 = (j − 1) + 2 = j + 1, ∀j ≥ 1.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :
ij = i0 + 1 × (j − 0) = j + 2, ∀j ≥ 0.
Alors : ij−1 = (j − 1) + 2 = j + 1, ∀j ≥ 1.
t[j + 1] = 5 × t[j] − 6 × t[j − 1], ∀j ≥ 1.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :
ij = i0 + 1 × (j − 0) = j + 2, ∀j ≥ 0.
Alors : ij−1 = (j − 1) + 2 = j + 1, ∀j ≥ 1.
t[j + 1] = 5 × t[j] − 6 × t[j − 1], ∀j ≥ 1.
Invariant de boucle :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :
ij = i0 + 1 × (j − 0) = j + 2, ∀j ≥ 0.
Alors : ij−1 = (j − 1) + 2 = j + 1, ∀j ≥ 1.
t[j + 1] = 5 × t[j] − 6 × t[j − 1], ∀j ≥ 1.
Invariant de boucle :

t[j] = 3j − 2j , ∀j ≥ 0

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :
ij = i0 + 1 × (j − 0) = j + 2, ∀j ≥ 0.
Alors : ij−1 = (j − 1) + 2 = j + 1, ∀j ≥ 1.
t[j + 1] = 5 × t[j] − 6 × t[j − 1], ∀j ≥ 1.
Invariant de boucle :

t[j] = 3j − 2j , ∀j ≥ 0
Preuve par récurrence.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :
ij = i0 + 1 × (j − 0) = j + 2, ∀j ≥ 0.
Alors : ij−1 = (j − 1) + 2 = j + 1, ∀j ≥ 1.
t[j + 1] = 5 × t[j] − 6 × t[j − 1], ∀j ≥ 1.
Invariant de boucle :

t[j] = 3j − 2j , ∀j ≥ 0
Preuve par récurrence.
Base :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :
ij = i0 + 1 × (j − 0) = j + 2, ∀j ≥ 0.
Alors : ij−1 = (j − 1) + 2 = j + 1, ∀j ≥ 1.
t[j + 1] = 5 × t[j] − 6 × t[j − 1], ∀j ≥ 1.
Invariant de boucle :

t[j] = 3j − 2j , ∀j ≥ 0
Preuve par récurrence.
Base :
On a : t[0] = 0 = 30 − 20 et t[1] = 1 = 31 − 21 .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :
ij = i0 + 1 × (j − 0) = j + 2, ∀j ≥ 0.
Alors : ij−1 = (j − 1) + 2 = j + 1, ∀j ≥ 1.
t[j + 1] = 5 × t[j] − 6 × t[j − 1], ∀j ≥ 1.
Invariant de boucle :

t[j] = 3j − 2j , ∀j ≥ 0
Preuve par récurrence.
Base :
On a : t[0] = 0 = 30 − 20 et t[1] = 1 = 31 − 21 .
Donc, la propriété est vraie pour j = 0 et j = 1.
Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes
Exercice 2

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction :
Soit j ≥ 2 et supposons que t[k] = 3k − 2k pour tout
entier k tel que 0 ≤ k < j.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction :
Soit j ≥ 2 et supposons que t[k] = 3k − 2k pour tout
entier k tel que 0 ≤ k < j.
Démontrons que t[j] = 3j − 2j .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction :
Soit j ≥ 2 et supposons que t[k] = 3k − 2k pour tout
entier k tel que 0 ≤ k < j.
Démontrons que t[j] = 3j − 2j .
On a : t[j] = t[(j − 1) + 1] avec j − 1 ≥ 1.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction :
Soit j ≥ 2 et supposons que t[k] = 3k − 2k pour tout
entier k tel que 0 ≤ k < j.
Démontrons que t[j] = 3j − 2j .
On a : t[j] = t[(j − 1) + 1] avec j − 1 ≥ 1.
D’après les faits,
t[j] = t[(j − 1) + 1] = 5 × t[j − 1] − 6 × t[j − 2].

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction :
Soit j ≥ 2 et supposons que t[k] = 3k − 2k pour tout
entier k tel que 0 ≤ k < j.
Démontrons que t[j] = 3j − 2j .
On a : t[j] = t[(j − 1) + 1] avec j − 1 ≥ 1.
D’après les faits,
t[j] = t[(j − 1) + 1] = 5 × t[j − 1] − 6 × t[j − 2].
D’après l’hypothèse de récurrence, on a :
t[j] = 5 × (3j−1 − 2j−1 ) − 6 × (3j−2 − 2j−2 ) = 3j − 2j .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Exploitation de l’invariant de boucle

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Exploitation de l’invariant de boucle


Il y a (n − 1) itérations.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Exploitation de l’invariant de boucle


Il y a (n − 1) itérations.
Donc, la dernière valeur de j est (n − 1).

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Exploitation de l’invariant de boucle


Il y a (n − 1) itérations.
Donc, la dernière valeur de j est (n − 1).
On en déduit alors que : t[(n − 1) + 1] = t[n] = 3n − 2n .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Exploitation de l’invariant de boucle


Il y a (n − 1) itérations.
Donc, la dernière valeur de j est (n − 1).
On en déduit alors que : t[(n − 1) + 1] = t[n] = 3n − 2n .
L’algorithme G (n) retourne la valeur t[n] = 3n − 2n .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Exploitation de l’invariant de boucle


Il y a (n − 1) itérations.
Donc, la dernière valeur de j est (n − 1).
On en déduit alors que : t[(n − 1) + 1] = t[n] = 3n − 2n .
L’algorithme G (n) retourne la valeur t[n] = 3n − 2n .
Conclusion : l’algorithme G (n) se termine et il est correct.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables
i, a, b, c : Entier;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables
i, a, b, c : Entier;
Début

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables
i, a, b, c : Entier;
Début
Si (n ≤ 1) Alors Retourner (n) FinSi

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables
i, a, b, c : Entier;
Début
Si (n ≤ 1) Alors Retourner (n) FinSi
a := 0;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables
i, a, b, c : Entier;
Début
Si (n ≤ 1) Alors Retourner (n) FinSi
a := 0; b := 1;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables
i, a, b, c : Entier;
Début
Si (n ≤ 1) Alors Retourner (n) FinSi
a := 0; b := 1; i := 2;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables
i, a, b, c : Entier;
Début
Si (n ≤ 1) Alors Retourner (n) FinSi
a := 0; b := 1; i := 2;
Tant Que (i ≤ n) Faire

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables
i, a, b, c : Entier;
Début
Si (n ≤ 1) Alors Retourner (n) FinSi
a := 0; b := 1; i := 2;
Tant Que (i ≤ n) Faire
c := 5 × b − 6 × a;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables
i, a, b, c : Entier;
Début
Si (n ≤ 1) Alors Retourner (n) FinSi
a := 0; b := 1; i := 2;
Tant Que (i ≤ n) Faire
c := 5 × b − 6 × a;
a := b;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables
i, a, b, c : Entier;
Début
Si (n ≤ 1) Alors Retourner (n) FinSi
a := 0; b := 1; i := 2;
Tant Que (i ≤ n) Faire
c := 5 × b − 6 × a;
a := b; b := c;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables
i, a, b, c : Entier;
Début
Si (n ≤ 1) Alors Retourner (n) FinSi
a := 0; b := 1; i := 2;
Tant Que (i ≤ n) Faire
c := 5 × b − 6 × a;
a := b; b := c; i := i + 1;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables
i, a, b, c : Entier;
Début
Si (n ≤ 1) Alors Retourner (n) FinSi
a := 0; b := 1; i := 2;
Tant Que (i ≤ n) Faire
c := 5 × b − 6 × a;
a := b; b := c; i := i + 1;
FinTQ

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables
i, a, b, c : Entier;
Début
Si (n ≤ 1) Alors Retourner (n) FinSi
a := 0; b := 1; i := 2;
Tant Que (i ≤ n) Faire
c := 5 × b − 6 × a;
a := b; b := c; i := i + 1;
FinTQ
Retourner (b);
Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes
Exercice 2

4 Sans utiliser aucun tableau, écrivons un algorithme itératif


H(n) qui soit équivalent à F (n) :
Fonction H(n : Entier) : Entier;
Variables
i, a, b, c : Entier;
Début
Si (n ≤ 1) Alors Retourner (n) FinSi
a := 0; b := 1; i := 2;
Tant Que (i ≤ n) Faire
c := 5 × b − 6 × a;
a := b; b := c; i := i + 1;
FinTQ
Retourner (b);
Fin
Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes
Exercice 2

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

5 Donnons la preuve de l’algorithme H(n) :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

5 Donnons la preuve de l’algorithme H(n) :


Si n = 0, l’algorithme G se termine et retourne
n = 0 = 30 − 20 .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

5 Donnons la preuve de l’algorithme H(n) :


Si n = 0, l’algorithme G se termine et retourne
n = 0 = 30 − 20 .
Si n = 1, l’algorithme G se termine et retourne
n = 1 = 31 − 21 .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

5 Donnons la preuve de l’algorithme H(n) :


Si n = 0, l’algorithme G se termine et retourne
n = 0 = 30 − 20 .
Si n = 1, l’algorithme G se termine et retourne
n = 1 = 31 − 21 .
Donc, si n = 0 ou n = 1, l’algorithme G se termine et il
est correct.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

5 Donnons la preuve de l’algorithme H(n) :


Si n = 0, l’algorithme G se termine et retourne
n = 0 = 30 − 20 .
Si n = 1, l’algorithme G se termine et retourne
n = 1 = 31 − 21 .
Donc, si n = 0 ou n = 1, l’algorithme G se termine et il
est correct.
Soit n ≥ 2. La boucle Tant Que sera effectuée
n − 2 + 1 = n − 1 fois, et son corps se termine.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

5 Donnons la preuve de l’algorithme H(n) :


Si n = 0, l’algorithme G se termine et retourne
n = 0 = 30 − 20 .
Si n = 1, l’algorithme G se termine et retourne
n = 1 = 31 − 21 .
Donc, si n = 0 ou n = 1, l’algorithme G se termine et il
est correct.
Soit n ≥ 2. La boucle Tant Que sera effectuée
n − 2 + 1 = n − 1 fois, et son corps se termine.
Par suite, l’algorithme se termine.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Les faits:

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Les faits:
a0 = 0;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Les faits:
a0 = 0;
b0 = 1;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Les faits:
a0 = 0;
b0 = 1;
i0 = 2;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Les faits:
a0 = 0;
b0 = 1;
i0 = 2;
c0 n’est pas défini.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Les faits:
a0 = 0;
b0 = 1;
i0 = 2;
c0 n’est pas défini.
cj = 5 × bj−1 − 6 × aj−1 , ∀j ≥ 1

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Les faits:
a0 = 0;
b0 = 1;
i0 = 2;
c0 n’est pas défini.
cj = 5 × bj−1 − 6 × aj−1 , ∀j ≥ 1
aj = bj−1 , ∀j ≥ 1

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Les faits:
a0 = 0;
b0 = 1;
i0 = 2;
c0 n’est pas défini.
cj = 5 × bj−1 − 6 × aj−1 , ∀j ≥ 1
aj = bj−1 , ∀j ≥ 1
bj = cj = 5 × bj−1 − 6 × aj−1 , ∀j ≥ 1

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Les faits:
a0 = 0;
b0 = 1;
i0 = 2;
c0 n’est pas défini.
cj = 5 × bj−1 − 6 × aj−1 , ∀j ≥ 1
aj = bj−1 , ∀j ≥ 1
bj = cj = 5 × bj−1 − 6 × aj−1 , ∀j ≥ 1
ij = ij−1 + 1, ∀j ≥ 1

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :
ij = j + 2, ∀j ≥ 0

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :
ij = j + 2, ∀j ≥ 0
bj = 5 × bj−1 − 6 × aj−1 , ∀j ≥ 1.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :
ij = j + 2, ∀j ≥ 0
bj = 5 × bj−1 − 6 × aj−1 , ∀j ≥ 1.
Invariant de boucle :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :
ij = j + 2, ∀j ≥ 0
bj = 5 × bj−1 − 6 × aj−1 , ∀j ≥ 1.
Invariant de boucle :

bj = 3j+1 − 2j+1 , ∀j ≥ 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

On en déduit que :
ij = j + 2, ∀j ≥ 0
bj = 5 × bj−1 − 6 × aj−1 , ∀j ≥ 1.
Invariant de boucle :

bj = 3j+1 − 2j+1 , ∀j ≥ 0.

Preuve par récurrence sur j (page suivante).

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Base

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Base
Pour j = 0, on a : b0 = 1 = 3 − 2 = 30+1 − 20+1 .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Base
Pour j = 0, on a : b0 = 1 = 3 − 2 = 30+1 − 20+1 .
Pour j = 1, on a : b1 = c1 = 5 × b0 − 6 × a0
= 5 × 1 − 6 × 0 = 5 = 32 − 22 = 31+1 − 21+1 .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Base
Pour j = 0, on a : b0 = 1 = 3 − 2 = 30+1 − 20+1 .
Pour j = 1, on a : b1 = c1 = 5 × b0 − 6 × a0
= 5 × 1 − 6 × 0 = 5 = 32 − 22 = 31+1 − 21+1 .
Donc, la propriété est vraie pour j = 0 et j = 1.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction
Hypothèse de récurrence : soit j ≥ 2 et supposons que
bk = 3k+1 − 2k+1 , pour tout entier k tel que 0 ≤ k < j.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction
Hypothèse de récurrence : soit j ≥ 2 et supposons que
bk = 3k+1 − 2k+1 , pour tout entier k tel que 0 ≤ k < j.
Démontrons que bj = 3j+1 − 2j+1 .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction
Hypothèse de récurrence : soit j ≥ 2 et supposons que
bk = 3k+1 − 2k+1 , pour tout entier k tel que 0 ≤ k < j.
Démontrons que bj = 3j+1 − 2j+1 .
D’après les faits, on a :
bj = 5 × bj−1 − 6 × aj−1 = 5 × bj−1 − 6 × bj−2 .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Induction
Hypothèse de récurrence : soit j ≥ 2 et supposons que
bk = 3k+1 − 2k+1 , pour tout entier k tel que 0 ≤ k < j.
Démontrons que bj = 3j+1 − 2j+1 .
D’après les faits, on a :
bj = 5 × bj−1 − 6 × aj−1 = 5 × bj−1 − 6 × bj−2 .
D’après l’hypothèse de récurrence, on a :
bj = 5 × (3j − 2j ) − 6 × (3j−1 − 2j−1 ) = 3j+1 − 2j+1 .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Exploitation de l’invariant de boucle

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Exploitation de l’invariant de boucle


Il y a (n − 1) itérations.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Exploitation de l’invariant de boucle


Il y a (n − 1) itérations.
Donc, la dernière valeur de j est (n − 1).

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Exploitation de l’invariant de boucle


Il y a (n − 1) itérations.
Donc, la dernière valeur de j est (n − 1).
On en déduit que : bn−1 = 3n − 2n .

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 2

Exploitation de l’invariant de boucle


Il y a (n − 1) itérations.
Donc, la dernière valeur de j est (n − 1).
On en déduit que : bn−1 = 3n − 2n .
Par suite, l’algorithme H(n) retourne la valeur
bn−1 = 3n − 2n , il est donc correct.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Soient a et b deux entiers positifs.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Soient a et b deux entiers positifs.


On veut calculer le quotient de a par b en n’effectuant
que des soustractions.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Soient a et b deux entiers positifs.


On veut calculer le quotient de a par b en n’effectuant
que des soustractions.

1 Établissons un algorithme itératif qui permet de résoudre


ce problème :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme itératif :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme itératif :
Fonction Quotient(a, b : Entier) : Entier;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme itératif :
Fonction Quotient(a, b : Entier) : Entier;
Variables

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme itératif :
Fonction Quotient(a, b : Entier) : Entier;
Variables
q : Entier;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme itératif :
Fonction Quotient(a, b : Entier) : Entier;
Variables
q : Entier;
Début

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme itératif :
Fonction Quotient(a, b : Entier) : Entier;
Variables
q : Entier;
Début
q := 0;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme itératif :
Fonction Quotient(a, b : Entier) : Entier;
Variables
q : Entier;
Début
q := 0;
Tant Que (a ≥ b) Faire

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme itératif :
Fonction Quotient(a, b : Entier) : Entier;
Variables
q : Entier;
Début
q := 0;
Tant Que (a ≥ b) Faire
a := a − b;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme itératif :
Fonction Quotient(a, b : Entier) : Entier;
Variables
q : Entier;
Début
q := 0;
Tant Que (a ≥ b) Faire
a := a − b;
q := q + 1;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme itératif :
Fonction Quotient(a, b : Entier) : Entier;
Variables
q : Entier;
Début
q := 0;
Tant Que (a ≥ b) Faire
a := a − b;
q := q + 1;
FinTQ

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme itératif :
Fonction Quotient(a, b : Entier) : Entier;
Variables
q : Entier;
Début
q := 0;
Tant Que (a ≥ b) Faire
a := a − b;
q := q + 1;
FinTQ
Retourner (q);

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme itératif :
Fonction Quotient(a, b : Entier) : Entier;
Variables
q : Entier;
Début
q := 0;
Tant Que (a ≥ b) Faire
a := a − b;
q := q + 1;
FinTQ
Retourner (q);
Fin

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

2 Donnons la preuve de l’algorithme :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

2 Donnons la preuve de l’algorithme :

Terminaison :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

2 Donnons la preuve de l’algorithme :

Terminaison :
Si a < b, la boucle ”Tant Que” n’est pas exécutée.
L’algorithme se termine.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

2 Donnons la preuve de l’algorithme :

Terminaison :
Si a < b, la boucle ”Tant Que” n’est pas exécutée.
L’algorithme se termine.
Si a ≥ b, la boucle sera exécutée au moins une fois.
Comme la valeur de a se décrémente de b unités à la fin
de chaque itération, la condition a ≥ b deviendra fausse
à un certain moment. L’algorithme se terminera alors.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Correction :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Correction :
Si a < b, l’algorithme retourne la valeur 0 = a/b.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Correction :
Si a < b, l’algorithme retourne la valeur 0 = a/b.
Supposons que a ≥ b. On a les faits suivants :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Correction :
Si a < b, l’algorithme retourne la valeur 0 = a/b.
Supposons que a ≥ b. On a les faits suivants :
1 a0 = a.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Correction :
Si a < b, l’algorithme retourne la valeur 0 = a/b.
Supposons que a ≥ b. On a les faits suivants :
1 a0 = a.
2 q0 = 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Correction :
Si a < b, l’algorithme retourne la valeur 0 = a/b.
Supposons que a ≥ b. On a les faits suivants :
1 a0 = a.
2 q0 = 0.
3 aj+1 = aj − b; ∀j ≥ 0

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Correction :
Si a < b, l’algorithme retourne la valeur 0 = a/b.
Supposons que a ≥ b. On a les faits suivants :
1 a0 = a.
2 q0 = 0.
3 aj+1 = aj − b; ∀j ≥ 0
4 qj+1 = qj + 1; ∀j ≥ 0

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Correction :
Si a < b, l’algorithme retourne la valeur 0 = a/b.
Supposons que a ≥ b. On a les faits suivants :
1 a0 = a.
2 q0 = 0.
3 aj+1 = aj − b; ∀j ≥ 0
4 qj+1 = qj + 1; ∀j ≥ 0
On en déduit alors que :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Correction :
Si a < b, l’algorithme retourne la valeur 0 = a/b.
Supposons que a ≥ b. On a les faits suivants :
1 a0 = a.
2 q0 = 0.
3 aj+1 = aj − b; ∀j ≥ 0
4 qj+1 = qj + 1; ∀j ≥ 0
On en déduit alors que :
1 qj = j, ∀j ≥ 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Correction :
Si a < b, l’algorithme retourne la valeur 0 = a/b.
Supposons que a ≥ b. On a les faits suivants :
1 a0 = a.
2 q0 = 0.
3 aj+1 = aj − b; ∀j ≥ 0
4 qj+1 = qj + 1; ∀j ≥ 0
On en déduit alors que :
1 qj = j, ∀j ≥ 0.
2 aj = a − jb, ∀j ≥ 0.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Soit k ≥ 1, le nombre d’itérations de la boucle ”Tant


Que”.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Soit k ≥ 1, le nombre d’itérations de la boucle ”Tant


Que”.
D’après les faits : ak = a − bk et qk = k.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Soit k ≥ 1, le nombre d’itérations de la boucle ”Tant


Que”.
D’après les faits : ak = a − bk et qk = k.
Posons r = a − bk.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Soit k ≥ 1, le nombre d’itérations de la boucle ”Tant


Que”.
D’après les faits : ak = a − bk et qk = k.
Posons r = a − bk.
Il est clair que a = bk + r et que 0 ≤ r < b.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Soit k ≥ 1, le nombre d’itérations de la boucle ”Tant


Que”.
D’après les faits : ak = a − bk et qk = k.
Posons r = a − bk.
Il est clair que a = bk + r et que 0 ≤ r < b.
Par suite, k est le quotient de a par b (théorème de la
division euclidienne).

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Soit k ≥ 1, le nombre d’itérations de la boucle ”Tant


Que”.
D’après les faits : ak = a − bk et qk = k.
Posons r = a − bk.
Il est clair que a = bk + r et que 0 ≤ r < b.
Par suite, k est le quotient de a par b (théorème de la
division euclidienne).
L’algorithme fournira la valeur qk = k = a/b.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Soit k ≥ 1, le nombre d’itérations de la boucle ”Tant


Que”.
D’après les faits : ak = a − bk et qk = k.
Posons r = a − bk.
Il est clair que a = bk + r et que 0 ≤ r < b.
Par suite, k est le quotient de a par b (théorème de la
division euclidienne).
L’algorithme fournira la valeur qk = k = a/b.
Il est donc correct.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Soit k ≥ 1, le nombre d’itérations de la boucle ”Tant


Que”.
D’après les faits : ak = a − bk et qk = k.
Posons r = a − bk.
Il est clair que a = bk + r et que 0 ≤ r < b.
Par suite, k est le quotient de a par b (théorème de la
division euclidienne).
L’algorithme fournira la valeur qk = k = a/b.
Il est donc correct.

3 Établissons un algorithme récursif qui permet de résoudre


ce problème :
Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes
Exercice 3

Algorithme récursif :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme récursif :
Fonction Quotient(a, b : Entier) : Entier;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme récursif :
Fonction Quotient(a, b : Entier) : Entier;
Début

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme récursif :
Fonction Quotient(a, b : Entier) : Entier;
Début
Si (a < b) Alors

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme récursif :
Fonction Quotient(a, b : Entier) : Entier;
Début
Si (a < b) Alors
Retourner (0);

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme récursif :
Fonction Quotient(a, b : Entier) : Entier;
Début
Si (a < b) Alors
Retourner (0);
Sinon

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme récursif :
Fonction Quotient(a, b : Entier) : Entier;
Début
Si (a < b) Alors
Retourner (0);
Sinon
Retourner (Quotient(a − b, b) + 1);

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme récursif :
Fonction Quotient(a, b : Entier) : Entier;
Début
Si (a < b) Alors
Retourner (0);
Sinon
Retourner (Quotient(a − b, b) + 1);
FinSi;

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Algorithme récursif :
Fonction Quotient(a, b : Entier) : Entier;
Début
Si (a < b) Alors
Retourner (0);
Sinon
Retourner (Quotient(a − b, b) + 1);
FinSi;
Fin

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

4 Donnons la preuve de l’algorithme :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

4 Donnons la preuve de l’algorithme :


Preuve par récurrence (forte).

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

4 Donnons la preuve de l’algorithme :


Preuve par récurrence (forte).
Base :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

4 Donnons la preuve de l’algorithme :


Preuve par récurrence (forte).
Base :
Si a < b, l’algorithme se termine et retourne 0 qui est le
quotient de a par b.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

4 Donnons la preuve de l’algorithme :


Preuve par récurrence (forte).
Base :
Si a < b, l’algorithme se termine et retourne 0 qui est le
quotient de a par b.
Donc, l’algorithme se termine et il est correct.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

4 Donnons la preuve de l’algorithme :


Preuve par récurrence (forte).
Base :
Si a < b, l’algorithme se termine et retourne 0 qui est le
quotient de a par b.
Donc, l’algorithme se termine et il est correct.
Induction :

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

4 Donnons la preuve de l’algorithme :


Preuve par récurrence (forte).
Base :
Si a < b, l’algorithme se termine et retourne 0 qui est le
quotient de a par b.
Donc, l’algorithme se termine et il est correct.
Induction :
Soit a ≥ b et supposons que l’algorithme se termine et il
est correct pour tout entier k avec 0 ≤ k < a.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

4 Donnons la preuve de l’algorithme :


Preuve par récurrence (forte).
Base :
Si a < b, l’algorithme se termine et retourne 0 qui est le
quotient de a par b.
Donc, l’algorithme se termine et il est correct.
Induction :
Soit a ≥ b et supposons que l’algorithme se termine et il
est correct pour tout entier k avec 0 ≤ k < a.
Montrons le pour l’entier a.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Comme a ≥ b, l’appel Quotient(a, b) va engendrer un


appel récursif avec les paramètres (a − b) et b.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Comme a ≥ b, l’appel Quotient(a, b) va engendrer un


appel récursif avec les paramètres (a − b) et b.
Puisque 0 ≤ a − b < a, l’appel Quotient(a − b, b) se
termine et renvoie la valeur q qui est le quotient de
(a − b) par a.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Comme a ≥ b, l’appel Quotient(a, b) va engendrer un


appel récursif avec les paramètres (a − b) et b.
Puisque 0 ≤ a − b < a, l’appel Quotient(a − b, b) se
termine et renvoie la valeur q qui est le quotient de
(a − b) par a.
Par conséquent, l’appel Quotient(a, b) se termine et
renvoie la valeur q + 1.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Comme a ≥ b, l’appel Quotient(a, b) va engendrer un


appel récursif avec les paramètres (a − b) et b.
Puisque 0 ≤ a − b < a, l’appel Quotient(a − b, b) se
termine et renvoie la valeur q qui est le quotient de
(a − b) par a.
Par conséquent, l’appel Quotient(a, b) se termine et
renvoie la valeur q + 1.
On a : a − b = bq + r , avec 0 ≤ r < b.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Comme a ≥ b, l’appel Quotient(a, b) va engendrer un


appel récursif avec les paramètres (a − b) et b.
Puisque 0 ≤ a − b < a, l’appel Quotient(a − b, b) se
termine et renvoie la valeur q qui est le quotient de
(a − b) par a.
Par conséquent, l’appel Quotient(a, b) se termine et
renvoie la valeur q + 1.
On a : a − b = bq + r , avec 0 ≤ r < b.
Donc, a = b(q + 1) + r , avec 0 ≤ r < b.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Comme a ≥ b, l’appel Quotient(a, b) va engendrer un


appel récursif avec les paramètres (a − b) et b.
Puisque 0 ≤ a − b < a, l’appel Quotient(a − b, b) se
termine et renvoie la valeur q qui est le quotient de
(a − b) par a.
Par conséquent, l’appel Quotient(a, b) se termine et
renvoie la valeur q + 1.
On a : a − b = bq + r , avec 0 ≤ r < b.
Donc, a = b(q + 1) + r , avec 0 ≤ r < b.
D’après le théorème de la division euclidienne, (q + 1) est
le quotient de a par b.

Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes


Exercice 3

Comme a ≥ b, l’appel Quotient(a, b) va engendrer un


appel récursif avec les paramètres (a − b) et b.
Puisque 0 ≤ a − b < a, l’appel Quotient(a − b, b) se
termine et renvoie la valeur q qui est le quotient de
(a − b) par a.
Par conséquent, l’appel Quotient(a, b) se termine et
renvoie la valeur q + 1.
On a : a − b = bq + r , avec 0 ≤ r < b.
Donc, a = b(q + 1) + r , avec 0 ≤ r < b.
D’après le théorème de la division euclidienne, (q + 1) est
le quotient de a par b.
Cela montre que l’algorithme Quotient(a, b) se termine et
il est correct.
Prof. Abdelmajid Dargham Chapitre 1 : Introduction aux algorithmes

S-ar putea să vă placă și