關於gcd

內容:

\(gcd(a,b)=gcd(b,a\% b)\)spa

用途:

這不廢話嘛,固然是用來求最大公約數啊code

證實:(這仍是四月份的時候cdx巨佬給我講的qwq)

\(d=gcd(a.b)\)
則有\(a=md,b=nd\), \(\Rightarrow\) \(a-b=(m-n)d\)
\(\because\) \(m\)\(n\)互質
\(\therefore\) \((m-n)\)\(n\)互質
\(\therefore\) \(gcd((m-n)d,nd)=d\)\(gcd(a-b,b)=d\)
\(\therefore\) \(gcd(a,b)=gcd(a-b,b)\)
\(\therefore\) \(gcd(a,b)=gcd(a\%b,b)\)class

關於爲何\(m-n\)\(n\)互質:
證實:假設\(m-n\)不與\(n\)互質,有\(gcd(m-n,n)=p,p\neq 1\)
\(m-n=xp,n=yp\)
\(\therefore\) \(m=(x+y)p\)
\(\therefore\) \(a=md=(x+y)pd,b=nd=ypd\)
\(\therefore\) \(a,b\)有公約數\(pd\)
$\because $ \(gcd(a,b)=d\)
\(\therefore\) \(p=1\)與假設矛盾
\(m-n\)\(n\)互質gc

Code:co

int gcd(int x, int y) {
    return y == 0 ? x : gcd(y, x % y);
}

謝謝收看,祝身體健康!math

相關文章
相關標籤/搜索