Difference between revisions of "Greatest common divisor"

From Free Pascal wiki
Jump to navigationJump to search
(removed Category:Code since it is in Category:Mathematics which is a sub-category thereof; removed unnecessary blanks)
Line 4: Line 4:
 
If numbers are 121 and 143 then greatest common divisor is 11.
 
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  
+
There are many methods to calculate this.
 +
For example, the division-based Euclidean algorithm version may be programmed:
  
== Function GreatestCommonDivisor ==
+
== <syntaxhighlight lang="pascal" enclose="none">function greatestCommonDivisor</syntaxhighlight> ==
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
+
function greatestCommonDivisor(a, b: Int64): Int64;
function GreatestCommonDivisor(a, b: Int64): Int64;
 
 
var
 
var
 
   temp: Int64;
 
   temp: Int64;
Line 21: Line 21:
 
   end;
 
   end;
 
   result := a
 
   result := a
end;  
+
end;
  
function GreatestCommonDivisor_EuclidsSubtractionMethod(a, b: Int64): Int64;
+
function greatestCommonDivisor_euclidsSubtractionMethod(a, b: Int64): Int64;
//only works with positive integers
 
 
begin
 
begin
 +
  // only works with positive integers
 
   if (a < 0) then a := -a;
 
   if (a < 0) then a := -a;
 
   if (b < 0) then b := -b;
 
   if (b < 0) then b := -b;
   if (a = 0) then Exit(b);
+
  // don't enter loop, since subtracting zero won't break condition
   if (b = 0) then Exit(a);  
+
   if (a = 0) then exit(b);
 +
   if (b = 0) then exit(a);
 
   while not (a = b) do
 
   while not (a = b) do
 
   begin
 
   begin
Line 37: Line 38:
 
     b := b - a;
 
     b := b - a;
 
   end;
 
   end;
   Result := a;
+
   result := a;
 
end;
 
end;
 +
</syntaxhighlight>
  
 +
== see also ==
  
</syntaxhighlight>
+
* [[Least common multiple|least common multiple]]
 +
* [[Mod|<syntaxhighlight lang="pascal" enclose="none">mod</syntaxhighlight> operator]]
 +
* <syntaxhighlight lang="pascal" enclose="none">mpz_gcd</syntaxhighlight> in [[gmp|GMP]] (GNU multiple precision)
  
== See also ==
+
== external references ==
  
* [http://rosettacode.org/wiki/Greatest_common_divisor#Pascal_.2F_Delphi_.2F_Free_Pascal Recursive example]
+
* [https://rosettacode.org/wiki/Greatest_common_divisor#Pascal_.2F_Delphi_.2F_Free_Pascal Recursive example]
* [[Least common multiple]]
 
* [[Mod]]
 
  
 
[[Category:Mathematics]]
 
[[Category:Mathematics]]
[[Category:Code]]
 

Revision as of 17:56, 13 February 2018

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

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;

function greatestCommonDivisor_euclidsSubtractionMethod(a, b: Int64): Int64;
begin
  // only works with positive integers
  if (a < 0) then a := -a;
  if (b < 0) then b := -b;
  // don't enter loop, since subtracting zero won't break condition
  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;

see also

external references