# GCD&LCM ## 最大公因數 求法:輾轉相除法,直到較小的數為0,則另一個數為最大公因數 ```cpp= int gcd(int a,int b){ //a<b if(a==0) return b; return gcd(b%a,a); } ``` 簡短版 ```cpp= int gcd(int a,int b){ //a<b return a==0?b:gcd(b%a,a); } ``` 直接呼叫C++內建函式 ```cpp= cout << gcd(12,16) << '\n'; //4 ``` ## 最小公倍數 兩數相乘,除以最大公因數 ``` lcm(a,b)=a*b/gcd(a,b) ``` ```cpp= int a=12,b=16; cout << a*b/gcd(a,b); //48 ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up