Greatest common divisor
English · Français · Polski · Suomi · Русский
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
- least common multiple
- mod operator
mpz_gcdin GMP (GNU multiple precision)
external references
From the Free Pascal wiki, page "Greatest common divisor", as it stood on 2023-12-13 after a change by Rvr, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Edited on wiki.freepascal.org by 7 people, 2015–2023
From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.
| When | Who | What changed |
|---|---|---|
| Djzepi | Created page | |
| Eny | (minor) | |
| Eny | (minor) | |
| Eny | (minor) | |
| Djzepi | ||
| Jwdietrich | categorization | |
| Eny | (minor) | |
| Eny | Replace superfluous link with more detailed examples (minor) | |
| Djzepi | ||
| Bart | Function GreatestCommonDivisor: add version based upon Euclids subtraction method | |
| Bart | Function GreatestCommonDivisor | |
| FTurtle | ||
| Kai Burghardt | removed Category:Code since it is in Category:Mathematics which is a sub-category thereof; removed unnecessary blanks | |
| Kai Burghardt | resolve Category: Pages using deprecated enclose attributes (minor) | |
| Rvr |
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
