google的开源库guava提供了计算最大公约数的实现使用非常简单,示例如下:
@Test
public void testGcd() {
int result = IntMath.gcd(1920, 1080);
System.out.printf("%d",result);
assertEquals(120, result);
}
如果参数类型是long,就用com.google.common.math.LongMath.gcd(long,long)
方法
mavn 引入guava依赖
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>