Jump to content

[PASCAL] Greatest Common Divisor


CouldnoT
 Share

Recommended Posts

Greatest Common Divisor

 

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:

 

How it works : 

 

Euclidian Algorithm: GCD (Greatest Common Divisor) Explained with C++ and  Java Examples

Script :

function greatestCommonDivisor(a, b: Int64): Int64;
var
  temp: Int64;
begin
  if a < b then 
  begin	
  	temp:= b;
	b:= a;
	a:= temp;
  end;
  while b <> 0 do // don't enter loop, since subtracting zero won't break condition
  begin
    temp := b;
    b := a mod b;
    a := temp
  end;
  result := a
end;

 

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.