在小學我們都知道,最大公約數是兩個共有的因子最大的一個。
exp
10和20的最大公約數是10
一般傳統方法爲
public static int gcd(int m,int n)
{
int r = 1;
while(r!=0)
{
r = m % n;
m =n;
n = r;
}
r = m;
return r;
}
但是大一點點的數字不好算了😒
exp
1345和1234的最大公約數
大家可以在這個鏈接下載第三方庫
第三方庫Apache Commons Math的API
下載後解壓導入idea中
下來是代碼,ArithmeticUtils函數有一個gcd方法
在這裏插入代碼片
package yanhaochen;
import org.apache.commons.math3.util.ArithmeticUtils;
public class hello {
public static void main(String[] args) {
int a = ArithmeticUtils.gcd(571,869);
int b = ArithmeticUtils.gcd(10,20);
System.out.println(a);
System.out.println(b);
}
}
```運行結果
![在這裏插入圖片描述](https://img-blog.csdnimg.cn/20200209173709307.jpg)
腳的喜歡就點個贊再走,謝謝,聽說點贊人都好運連連🤞🤞🤞