question01

Top  Previous  Next

tipbulbImplement a static method to perform this problem: Two plots of land with areas of 23,500 m² (square meters) and 14,100m² (square meters) are divided into lots, as large as possible, all in the same area. The total number of lots obtained, is:

 

Note: Here the exit statement exits the method returning the left value.

   A static method (it does not need an instance of Utilities to be called) the euclidean algorithm for computing the greatest common divisor, finding a number both left and right can be divided by. This method can be reused from multiple places in code.

mytoggle_plus1See the solution:

type

  Utilities = class

  public

    class method GCD(left, right: Integer): Integer;

  end;

 

implementation

 

{ Utilities }

class method Utilities.GCD(left, right: Integer): Integer;

begin

  // repeat everything from begin to end as long as right is not 0.

  while right <> 0 do begin

    // defines a new variable that returns left modulus right.

    var rem := left mod right;

    // sets left to right.

    left := right;

    // sets right to the earlier calculated remainder.

    right := rem;

  end;

  // return the left value.

  exit left;

end;

 

var x : Integer;

begin

x:=  Utilities.GCD(2350014100);

WriteLn(x);

end;

 

//solution: 4700