greatest common divisor, take care special case j = 0