Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cours
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
aurelien.boyer
cours
Commits
19ec52ca
Verified
Commit
19ec52ca
authored
2 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
moved pgcd to cours_3
parent
f038c41e
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
slides/cours_2.md
+1
-148
1 addition, 148 deletions
slides/cours_2.md
with
1 addition
and
148 deletions
slides/cours_2.md
+
1
−
148
View file @
19ec52ca
...
...
@@ -264,7 +264,7 @@ Si un nombre, `p`, est multiple de `a` et de `b` alors il peut s'écrire `p = a
```
C
int ppcm(int a, int b) {
for (i in [1, b]) {
if a * i
is
divisible
by
b {
if a * i
est
divisible
par
b {
return a * i
}
}
...
...
@@ -312,153 +312,6 @@ int main() {
}
```
# Le calcul du PGCD (1/5)
## Définition
Le plus grand commun diviseur (PGCD) de deux nombres entiers non nuls est le
plus grand entier qui les divise en même temps.
## Exemples:
```
C
PGCD(3, 4) = 1,
PGCD(4, 6) = 2,
PGCD(5, 15) = 5.
```
. . .
## Mathématiquement
Décomposition en nombres premiers:
$$
36 = 2^2
\c
dot 3^2,
\q
uad 90=2
\c
dot 5
\c
dot 3^2,
$$
On garde tous les premiers à la puissance la plus basse
$$
PGCD(36, 90)=2^{
\m
in{1,2}}
\c
dot 3^{
\m
in{2,2}}
\c
dot 5^{
\m
in{0,1}}=18.
$$
# Le calcul du PGCD (2/5)
## Algorithme
Par groupe de 3 (5-10min):
*
réfléchissez à un algorithme alternatif donnant le PGCD de deux nombres;
*
écrivez l'algorithme en pseudo-code.
. . .
## Exemple d'algorithme
```
C
PGCD(36, 90):
90 % 36 != 0 // otherwise 36 would be PGCD
90 % 35 != 0 // otherwise 35 would be PGCD
90 % 34 != 0 // otherwise 34 would be PGCD
...
90 % 19 != 0 // otherwise 19 would be PGCD
90 % 18 == 0 // The end!
```
*
18 modulos, 18 assignations, et 18 comparaisons.
# Le calcul du PGCD (3/5)
## Transcrivez cet exemple en algorithme (groupe de 3) et codez-le (5-10min)!
. . .
## Optimisation
*
Combien d'additions / comparaisons au pire?
*
Un moyen de le rendre plus efficace?
. . .
## Tentative de correction
```
C
void main() {
int n = 90, m = 78;
int gcd = 1;
for (int div = n; div >= 2; div--) { // div = m, sqrt(n)
if (n % div == 0 && m % div == 0) {
gcd = div;
break;
}
}
printf("Le pgcd de %d et %d est %d\n", n, m, gcd);
}
```
# Le calcul du PGCD (4/5)
## Réusinage: l'algorithme d'Euclide
`Dividende = Diviseur * Quotient + Reste`
```
C
PGCD(35, 60):
35 = 60 * 0 + 35 // 60 -> 35, 35 -> 60
60 = 35 * 1 + 25 // 35 -> 60, 25 -> 35
35 = 25 * 1 + 10 // 25 -> 35, 20 -> 25
25 = 10 * 2 + 5 // 10 -> 25, 5 -> 10
10 = 5 * 2 + 0 // PGCD = 5!
```
. . .
## Algorithme
Par groupe de 3 (5-10min):
*
analysez l'exemple ci-dessus;
*
transcrivez le en pseudo-code.
# Le calcul du PGCD (5/5)
## Pseudo-code
```
C
int pgcd(int a, int b) {
tmp_n = n
tmp_m = m
while (tmp_m does not divide tmp_n) {
tmp = tmp_n
tmp_n = tmp_m
tmp_m = tmp modulo tmp_m
}
return tmp_m
}
```
# Le code du PGCD de 2 nombres
## Implémentez le pseudo-code et postez le code sur matrix (5min).
. . .
## Un corrigé possible
```
C
#include <stdio.h>
void main() {
int n = 90;
int m = 78;
printf("n = %d et m = %d\n", n, m);
int tmp_n = n;
int tmp_m = m;
while (tmp_n%tmp_m > 0) {
int tmp = tmp_n;
tmp_n = tmp_m;
tmp_m = tmp % tmp_m;
}
printf("Le pgcd de %d et %d est %d\n", n, m, tmp_m);
}
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment