Proposition Indic Sol
-
Upload
med-mohamed -
Category
Documents
-
view
212 -
download
0
description
Transcript of Proposition Indic Sol
![Page 1: Proposition Indic Sol](https://reader035.fdocuments.fr/reader035/viewer/2022072009/55cf92c4550346f57b996d41/html5/thumbnails/1.jpg)
Proposition d’une indication de solution de l’examen d’Algorithmique-2
2014/2015
Exercice 2 :
fonction nbr_parfait(n :Entier) : Entier variables i,s : Entiers Début
s � 1 Pour i allant de 2 à div(n,2) faire si (mod(n,i) = 0)
s � s + i Finsi FinPour
si(s=n) retourner 1 retourner 0
Fin
Exercice 3 :
Nombres parfaits :
• 28 = 1 + 2 + 4 + 7 + 14
• 496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248
Exercice 5 :
1. prod_shift_iter
fonction prod_shift_iter (N : Entier , M : Entier) : Entier Double
variables S Début S � 0
Si N=0 OU M=0 retourner 0 FinSi
TantQue N <> 1 Si mod(N,2) <> 0
S � S+M FinSi N � div(N,2)
M � M*2 FinTantQue
S � S+M retourner S Fin
2. complexité : O(log(N))
![Page 2: Proposition Indic Sol](https://reader035.fdocuments.fr/reader035/viewer/2022072009/55cf92c4550346f57b996d41/html5/thumbnails/2.jpg)
3. prod_shift_rec
fonction prod_shift_rec (N : Entier , M : Entier) : Entier Double
Début
Si N=0 OU M=0 retourner 0
FinSi
Si mod(N,2) <> 0 retourner M + prod_shift_rec(div(N,2), 2*M)
Sinon retourner prod_shift_rec(div(N,2), 2*M)
FinSi
Fin
4.
variables x,y : Entiers
variable prod : Entier Double Début Ecrire ("Saisir x : ")
Lire (x) Ecrire ("Saisir y : ") Lire (y)
si x <= y alors prod � prod_shift_iter(x,y)
sinon
prod � prod_shift_iter(y,x) FinSi Ecrire ("Le résultat de multiplication par SHIFTS : ", prod)
Fin
Exercice 6 : longueur récursive
fonction long_rec(ch : chaine de caracteres) : Entier
Début Si ch[0] = ‘\0’ retourner 0
FinSi retourner 1 + long_rec(ch+1) Fin