Difference between revisions of "Greatest common divisor/fr"

From Lazarus wiki
Jump to navigationJump to search
(Created page with "{{Greatest common divisor}} Le plus grand diviseur commun (PGDC) de deux entiers est le plus grand entier qui les divise les deux. Si les nombres sont 121 et 143 alors le PGD...")
 
m (Fixed syntax highlighting)
 
Line 8: Line 8:
 
== Fonction GreatestCommonDivisor ==
 
== Fonction GreatestCommonDivisor ==
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
  
 
function GreatestCommonDivisor(a, b: Int64): Int64;
 
function GreatestCommonDivisor(a, b: Int64): Int64;
Line 46: Line 46:
 
* [[Least common multiple/fr|plus petit multiple commun]]
 
* [[Least common multiple/fr|plus petit multiple commun]]
 
* [[Mod/fr|Mod]]
 
* [[Mod/fr|Mod]]
<br>
 

Latest revision as of 13:13, 16 February 2020

English (en) suomi (fi) français (fr) polski (pl) русский (ru)

Le plus grand diviseur commun (PGDC) de deux entiers est le plus grand entier qui les divise les deux. Si les nombres sont 121 et 143 alors le PGDC est 11 (121=11x11 et 143=11*13)

Il existe plusieurs méthodes pour le calculer. P.ex., l'algorithme d'Euclide basé sur la division peut être implémenté.

Fonction GreatestCommonDivisor

function GreatestCommonDivisor(a, b: Int64): Int64;
var
  temp: Int64;
begin
  while b <> 0 do
  begin
    temp := b;
    b := a mod b;
    a := temp
  end;
  result := a
end; 

function GreatestCommonDivisor_EuclidsSubtractionMethod(a, b: Int64): Int64;
//only works with positive integers
begin
  if (a < 0) then a := -a;
  if (b < 0) then b := -b;
  if (a = 0) then Exit(b);
  if (b = 0) then Exit(a); 
  while not (a = b) do
  begin
    if (a > b) then
     a := a - b
    else
     b := b - a;
  end;
  Result := a;
end;

Voir aussi