• Aucun résultat trouvé

if (p=1) then return q

N/A
N/A
Protected

Academic year: 2022

Partager "if (p=1) then return q "

Copied!
1
0
0

Texte intégral

(1)

Informatique MPSI B Hoche

Paysan russe La multiplication du paysan russe est une méthode de calcul d’un produit de deux entiers qui n’utilise que des additions, la soustraction de 1, le calcul de la parité d’un nombre, la division par 2 d’un nombre pair.

Écrire une procédure récursivemultrus(p,q)qui renvoie le produit de pet qen utilisant cette méthode.

Un corrigé dans lafeuille de calculà télécharger.

Corrigé

multrus:=proc(p,q)

local truc, par_p, par_q;

#print (cat("multrus ", p , q));

if (p=1) then return q ; elif (q=1) then

return p;

else

par_p := p mod 2 ; par_q := q mod 2 ;

if (par_p=0) and (par_q=0) then truc := multrus(p/2,q/2);

return truc + truc + truc + truc;

fi;

if (par_p=0) and (par_q=1) then truc := multrus(p/2,(q-1)/2);

return truc + truc + truc + truc + p;

fi;

if (par_p=1) and (par_q=0) then truc := multrus((p-1)/2,q/2);

return truc + truc + truc + truc + q;

fi;

if (par_p=1) and (par_q=1) then truc := multrus((p-1)/2,(q-1)/2);

return truc + truc + truc + truc + p + q - 1;

fi;

fi;

end proc:

Cette création est mise à disposition selon le Contrat

Paternité-Pas d’utilisations commerciale-Partage des Conditions Initiales à l’Identique 2.0 France disponible en ligne http://creativecommons.org/licenses/by-nc-sa/2.0/fr/

1 Rémy Nicolai Apayrus

Références

Documents relatifs

[r]

[r]

initialisation (par lecture) des variables d'entrée ; writeln est facultatif, readln indispensable. pas de point virgule avant

[r]

[r]

[r]

JorrrpN: On the use of Löwner's functions for finding inequalities for the first coefficient body of bounded univalent functions. - Reports of the Department of

[r]