calculate greatest common divisor