Difference between revisions of "Greatest common divisor"

From Lazarus wiki
Jump to navigationJump to search
(categorization)
m
Line 8: Line 8:
 
<syntaxhighlight>
 
<syntaxhighlight>
  
function GreatestCommonDivisor( a, b: Int64 ): Int64;
+
function GreatestCommonDivisor(a, b: Int64): Int64;
 
var
 
var
   temp : Int64;
+
   temp: Int64;
 
begin
 
begin
 
   while b <> 0 do
 
   while b <> 0 do
    begin
+
  begin
      temp := b ;
+
    temp := b;
      b := a mod b ;
+
    b := a mod b;
      a := temp ;
+
    a := temp
    end;
+
  end;
   result := a;
+
   result := a
 
end;  
 
end;  
  

Revision as of 14:36, 10 March 2015

The greatest common divisor of two integers is the largest integer that divides them both. If numbers are 121 and 143 then greatest common divisor is 11.

There are many methods to calculate this. For example, the division-based Euclidean algorithm version may be programmed

Function 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;

See also