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
jeremy.meissner
cours
Commits
32ccc9a3
Verified
Commit
32ccc9a3
authored
3 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
update ppcm
parent
cea14430
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
+120
-3
120 additions, 3 deletions
slides/cours_2.md
with
120 additions
and
3 deletions
slides/cours_2.md
+
120
−
3
View file @
32ccc9a3
...
...
@@ -59,7 +59,7 @@ int main() {
-
Initialisation, addition et multiplication de matrices
-
Couverture de la reine
# Le calcul du PPCM (1/
2
)
# Le calcul du PPCM (1/
6
)
## Définition
...
...
@@ -69,12 +69,75 @@ nombres.
Exemples:
```
bash
```
C
PPCM(3, 4) = 12,
PPCM(4, 6) = 12,
PPCM(5, 15) = 15.
```
. . .
## 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 élevée
$$
PPCM(36, 45)=2^2
\c
dot 3^2
\c
dot 5=180.
$$
# Le calcul du PPCM (2/6)
## Exemple d'algorithme
```
C
36 < 90 // 36 + 36
72 < 90 // 72 + 36
108 > 90 // 90 + 90
108 < 180 // 108 + 36
144 < 180 // 144 + 36
180 = 180 // The End!
```
*
5 additions, 5 assignations, et 6 comparaisons.
. . .
## Transcrivez cet exemple en algorithme (groupe de 3)
. . .
## et codez-le!
. . .
# Le calcul du PPCM (3/6)
## Tentative de correction
```
C
int main() {
int m = 15, n = 12;
int mult_m = m, mult_n = n;
while (mult_m != mult_n) {
if (mult_m > mult_n) {
mult_n += n;
} else {
mult_m += m;
}
}
printf("Le ppcm de %d et %d est %d\n", n, m, mult_m);
}
```
Combien d'additions / comparaisons au pire?
# Le calcul du PPCM (4/6)
## Comment décrire une fonction qui ferait ce calcul (arguments, sorties)?
. . .
...
...
@@ -94,7 +157,31 @@ Par groupe de 3:
*
réfléchissez à un algorithme donnant le PPCM de deux nombres;
*
écrivez l'algorithme en pseudo-code.
# Le calcul du PPCM (1/2)
# Le calcul du PPCM (5/6)
Si un nombre,
`p`
, est multiple de
`a`
et de
`b`
alors il peut s'écrire
`p = a
* i = b * j`
ou encore
`p / a = i`
et
`p / b = j`
.
<!-- Si un nombre, $p$, est multiple de $a$ et de $b$ alors il peut s'écrire -->
<!-- $$ -->
<!-- p = a \cdot i = b \cdot j, -->
<!-- $$ -->
<!-- ou encore $p / a = i$ et $p / b = j$. -->
## Pseudo-code
```
C
int ppcm(int a, int b) {
for (i in [1, b]) {
if a * i is divisible by b {
return a * i
}
}
}
```
# Le calcul du PPCM (6/6)
Si un nombre,
`p`
, est multiple de
`a`
et de
`b`
alors il peut s'écrire
`p = a
* i = b * j`
ou encore
`p / a = i`
et
`p / b = j`
.
...
...
@@ -160,3 +247,33 @@ int main() {
}
```
```
C
#include <stdio.h>
void main() {
int n = 90;
int m = 78;
printf("n = %d et m = %d\n", n, m);
// algorithme naif
int gcd = 1;
for (int div = n; div >= 2; div--) {
if (n % div == 0 && m % div == 0) {
gcd = div;
break;
}
}
printf("Le pgcd de %d et %d est %d\n",n,m,gcd);
// algorithme d'Euclide
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