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
algorithmique
cours
Commits
60545276
Verified
Commit
60545276
authored
3 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
added gitlab-ci and corrected typo
parent
32bc88e9
No related branches found
No related tags found
No related merge requests found
Pipeline
#14538
passed
3 years ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.gitlab-ci.yml
+59
-0
59 additions, 0 deletions
.gitlab-ci.yml
slides/cours_2.md
+147
-48
147 additions, 48 deletions
slides/cours_2.md
with
206 additions
and
48 deletions
.gitlab-ci.yml
0 → 100644
+
59
−
0
View file @
60545276
image
:
omalaspinas/pandoc:latest
variables
:
GIT_SUBMODULE_STRATEGY
:
recursive
before_script
:
##
## Install ssh-agent if not already installed, it is required by Docker.
## (change apt-get to yum if you use an RPM-based image)
##
-
'
which
ssh-agent
||
(pacman
-S
--noconfirm
openssh)'
-
'
which
rsync
||
(pacman
-S
--noconfirm
rsync)'
##
## Run ssh-agent (inside the build environment)
##
-
eval $(ssh-agent -s)
##
## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
## We're using tr to fix line endings which makes ed25519 keys work
## without extra base64 encoding.
## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
##
-
echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
##
## Create the SSH directory and give it the right permissions
##
-
mkdir -p ~/.ssh
-
chmod 700 ~/.ssh
##
## Add host id to known_hosts
##
-
echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
-
chmod 644 ~/.ssh/known_hosts
build_only
:
script
:
-
cd slides
-
make
-
make deploy
-
rsync -avz algo_cours ur1bg_malas@ur1bg.ftp.infomaniak.com:web/malaspinas/
build_artifacts
:
script
:
-
cd slides
-
make
artifacts
:
paths
:
-
"
*.pdf"
-
"
*.html"
only
:
-
tags
This diff is collapsed.
Click to expand it.
slides/cours_2.md
+
147
−
48
View file @
60545276
...
@@ -59,7 +59,7 @@ int main() {
...
@@ -59,7 +59,7 @@ int main() {
-
Initialisation, addition et multiplication de matrices
-
Initialisation, addition et multiplication de matrices
-
Couverture de la reine
-
Couverture de la reine
# Le calcul du PPCM (1/
6
)
# Le calcul du PPCM (1/
5
)
## Définition
## Définition
...
@@ -86,14 +86,15 @@ $$
...
@@ -86,14 +86,15 @@ $$
$$
$$
On garde tous les premiers à la puissance la plus élevée
On garde tous les premiers à la puissance la plus élevée
$$
$$
PPCM(36,
45
)=2^2
\c
dot 3^2
\c
dot 5=180.
PPCM(36,
90
)=2^2
\c
dot 3^2
\c
dot 5=180.
$$
$$
# Le calcul du PPCM (2/
6
)
# Le calcul du PPCM (2/
5
)
## Exemple d'algorithme
## Exemple d'algorithme
```
C
```
C
PPCM(36, 90):
36 < 90 // 36 + 36
36 < 90 // 36 + 36
72 < 90 // 72 + 36
72 < 90 // 72 + 36
108 > 90 // 90 + 90
108 > 90 // 90 + 90
...
@@ -112,10 +113,8 @@ $$
...
@@ -112,10 +113,8 @@ $$
## et codez-le!
## et codez-le!
. . .
# Le calcul du PPCM (3/
6
)
# Le calcul du PPCM (3/
5
)
## Tentative de correction
## Tentative de correction
...
@@ -134,15 +133,17 @@ int main() {
...
@@ -134,15 +133,17 @@ int main() {
}
}
```
```
Combien d'additions / comparaisons au pire?
. . .
*
Combien d'additions / comparaisons au pire?
# Le calcul du PPCM (4/
6
)
# Le calcul du PPCM (4/
5
)
## Comment décrire une fonction qui ferait ce calcul (arguments, sorties)?
##
Réusinage:
Comment décrire une fonction qui ferait ce calcul (arguments, sorties)?
. . .
. . .
En
`C`
on pourrait l
e
décrire comme
En
`C`
on pourrait l
a
décrire comme
```
C
```
C
int ppcm(int a, int b); // La **signature** de cette fonction
int ppcm(int a, int b); // La **signature** de cette fonction
...
@@ -154,34 +155,12 @@ int ppcm(int a, int b); // La **signature** de cette fonction
...
@@ -154,34 +155,12 @@ int ppcm(int a, int b); // La **signature** de cette fonction
Par groupe de 3:
Par groupe de 3:
*
réfléchissez à un algorithme donnant le PPCM de deux nombres;
*
réfléchissez à un algorithme
alternatif
donnant le PPCM de deux nombres;
*
écrivez l'algorithme en pseudo-code.
*
écrivez l'algorithme en pseudo-code.
# Le calcul du PPCM (5/
6
)
# Le calcul du PPCM (5/
5
)
Si un nombre,
`p`
, est multiple de
`a`
et de
`b`
alors il peut s'écrire
`p = a
## Indication
* 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
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`
.
* i = b * j`
ou encore
`p / a = i`
et
`p / b = j`
.
...
@@ -192,6 +171,8 @@ Si un nombre, `p`, est multiple de `a` et de `b` alors il peut s'écrire `p = a
...
@@ -192,6 +171,8 @@ Si un nombre, `p`, est multiple de `a` et de `b` alors il peut s'écrire `p = a
<!-- $$ -->
<!-- $$ -->
<!-- ou encore $p / a = i$ et $p / b = j$. -->
<!-- ou encore $p / a = i$ et $p / b = j$. -->
. . .
## Pseudo-code
## Pseudo-code
```
C
```
C
...
@@ -218,7 +199,7 @@ int ppcm(int a, int b) {
...
@@ -218,7 +199,7 @@ int ppcm(int a, int b) {
#include <stdlib.h>
#include <stdlib.h>
int main() {
int main() {
int n = 15,m = 12;
int n = 15,
m = 12;
int i = 1;
int i = 1;
while (n * i % m != 0) {
while (n * i % m != 0) {
i++;
i++;
...
@@ -247,34 +228,152 @@ int main() {
...
@@ -247,34 +228,152 @@ int main() {
}
}
```
```
# Le PGCD
# Le calcul du PGCD (1/N)
## 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
```
C
#include <stdio.h>
PGCD(3, 4) = 1,
void main() {
PGCD(4, 6) = 2,
int n = 90;
PGCD(5, 15) = 5.
int m = 78;
```
printf("n = %d et m = %d\n", n, m);
. . .
## 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/6)
## Algorithme
Par groupe de 3:
*
réfléchissez à un algorithme alternatif donnant le PPCM de deux nombres;
*
écrivez l'algorithme en pseudo-code.
// algorithme naif
. . .
## 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/6)
## Transcrivez cet exemple en algorithme (groupe de 3) et codez-le!
. . .
## 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;
int gcd = 1;
for (int div = n; div >= 2; div--) {
for (int div = n; div >= 2; div--) {
// div = m, sqrt(n)
if (n % div == 0 && m % div == 0) {
if (n % div == 0 && m % div == 0) {
gcd = div;
gcd = div;
break;
break;
}
}
}
}
printf("Le pgcd de %d et %d est %d\n",n,m,gcd);
printf("Le pgcd de %d et %d est %d\n", n, m, gcd);
}
```
// algorithme d'Euclide
# Le calcul du PGCD (4/6)
## 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:
*
analysez l'exemple ci-dessus;
*
transcrivez le en pseudo-code.
# Le calcul du PGCD (5/6)
## 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.
. . .
## 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_n = n;
int tmp_m = m;
int tmp_m = m;
while (tmp_n
%
tmp_m > 0) {
while (tmp_n
%
tmp_m > 0) {
int tmp = tmp_n;
int tmp = tmp_n;
tmp_n = tmp_m;
tmp_n = tmp_m;
tmp_m = tmp%tmp_m;
tmp_m = tmp%tmp_m;
}
}
printf("Le pgcd de %d et %d est %d\n", n, m, 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