For faster navigation, this Iframe is preloading the Wikiwand page for 最大公因數.

最大公因數

最大公因數(英語:highest common factorhcf)也稱最大公約數(英語:greatest common divisorgcd)是數學詞彙,指能够整除多個非零整數的最大正整数。例如8和12的最大公因数为4。

整数序列的最大公因数可以記為

最大公因数的值至少為1,例如;最大則為該組整數中絕對值最小的絕對值,例如

求兩個整數最大公因數主要的方法:

  • 列舉法:分別列出兩整數的所有因數,並找出最大的公因數。
  • 質因數分解:分別列出兩數的質因數分解式,並計算共同項的乘積
  • 短除法:兩數除以其共同質因數,直到兩數互質時,所有除數的乘積即為最大公因數。

兩個整數的最大公因數和最小公倍數lcm)的關係為:

兩個整數的最大公因數可用於計算兩數的最小公倍數,或分數化簡成最簡分數

兩個整數的最大公因數和最小公倍數中存在分配律

直角坐標中,兩頂點的線段會通過個格子點。

概述

[编辑]

例子

[编辑]

54和24的最大公因数是多少?

数字54可以表示為几组不同正整數的乘積:

故54的正因數為

同樣地,24可以表示為:

故24的正因數為

这两组数列中的共同元素即为54和24的公因数

其中的最大數6即為54和24的最大公因數,記為:

互质数

[编辑]

如果两数的最大公因数为1,那么这两个数互質。例如,9和28就是互质数。

几何角度的说明

[编辑]
"瘦长的矩形区域,划分成了正方形的网格,宽两格、高五格。"
24乘60的矩形被十个12乘12的正方形格子完全覆盖,即12为24和60的最大公因数。推而广之,如果cab的最大公因数,那么ab的矩形就可以被若干个边长为c的正方形格子完全覆盖。

假设有一个大小为24乘60的矩形区域,这个区域可以按照不同的大小划分正方形网格:1乘1、2乘2、3乘3、4乘4、6乘6、12乘12。因此,12是24和60的最大公因数。大小为24乘60的矩形区域,可以按照12乘12的大小划分正方形网格,一边有两格()、另一边有五格()。

计算

[编辑]

质因数分解法

[编辑]

可以通过質因數分解来计算最大公因数。例如计算,可以先进行质因数分解 ,因为其中表达式的「重合」,所以。实践中,这种方法只在数字比较小时才可行,因为对较大数进行质因数分解通常会耗费大量的时间。

再举一个用文氏图表示的例子,计算48和180的最大公因数。首先对这两个数进行质因数分解:

它们之中的共同元素是两个2和一个3:

[1]
最小公倍数
最大公因数

辗转相除法

[编辑]

相比质因数分解法,辗转相除法的效率更高。

计算时,先将48除以18得到2、余数12,然后再将18除以12得到商1、余数6,再将12除以6得到商2、余数0,即得到最大公因数6。我们只关心每次除法的余数是否为0,为0即表示得到答案。这一算法更正式的描述是这样的:

其中

如果参数都大于0,那么该算法可以写成更简单的形式:

,
如果 a > b
如果 b > a
使用辗转相除法计算62和36的最大公因数2的演示动画。

性质

[编辑]
  • 任意ab的公因数都是因數
  • 函数满足交换律
  • 函数满足结合律

程式代碼

[编辑]

以下使用輾轉相除法實現。

C#

[编辑]
private int GCD(int a, int b) {
	if(0 != b) while(0 != (a %= b) && 0 != (b %= a));
	return a + b;
}

C++

[编辑]

运行时计算实现:

template<typename T>
T GCD(T a, T b) {
	if(b) while((a %= b) && (b %= a));
	return a + b;
}

编译时计算实现:

#include <iostream>
#include <type_traits>

template<typename T, std::enable_if_t<std::is_integral<T>::value, T> a, std::enable_if_t<std::is_integral<T>::value, T> b>
struct HCF{
public:
    static const T value=HCF<T, (a>b? b: a), (a>b? a%b: b%a)>::value;
};
template<typename T, std::enable_if_t<std::is_integral<T>::value, T> a>
struct HCF<T, a, 0>{
public:
    static const T value=a;
};
int main(){
    std::wcout<<HCF<int, 12, 64>::value<<std::endl;//Output: 4
}
int GCD(int a, int b) {
	if (b) while((a %= b) && (b %= a));
	return a + b;
}

Java

[编辑]
private int GCD(int a, int b) {
    if (b==0) return a; 
	return GCD(b, a % b);
}

JavaScript

[编辑]
const GCD = (a, b) => b ? GCD(b, a % b) : a;

Python

[编辑]
GCD = lambda a, b: (a if b == 0 else GCD(b, a % b))

# or

def GCD(a, b):
    if b == 0:
        return a
    return GCD(b, a % b)

政治用法

[编辑]
此章节需要扩充。 (2020年8月30日)

最大公約數又指一社會中不同陣營勉強所達之共同利益。

参考文献

[编辑]

外部链接

[编辑]

参见

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
最大公因數
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?