最大公约数

最大公约数(greatest common divisor,eq?gcd),指能够整除多个非零整数的最大正整数。

求最大公约数

1.素数分解法

素数,又叫质数(Prime number),指大于1的正整数中只有1和该数本身2个正约数的数。大于1的自然数若不是素数就是合数。因此一个自然数可以分解为多个素数的积。

比如求eq?%5Cgcd%7B%5Cleft%2848%2C180%20%5Cright%29%7D

eq?%5Cbegin%7Baligned%7D%2048%26%3D2%5E%7B4%7D%5Ctimes%203%5E%7B1%7D%5C%5C%20180%26%3D2%5E%7B2%7D%5Ctimes%203%5E%7B2%7D%5Ctimes%205%5E%7B1%7D%20%5Cend%7Baligned%7D%5CRightarrow%20%5Cbegin%7Baligned%7D%2048%26%3D%7B%5Cleft%282%5E%7B2%7D%5Ctimes3%5E%7B1%7D%20%5Cright%20%29%7D%5Ctimes2%5E%7B2%7D%5C%5C%20180%26%3D%7B%5Cleft%282%5E%7B2%7D%5Ctimes3%5E%7B1%7D%20%5Cright%20%29%7D%5Ctimes3%5E%7B1%7D%5Ctimes5%5E%7B1%7D%20%5Cend%7Baligned%7D

eq?%5Cgcd%7B%5Cleft%2848%2C180%20%5Cright%29%7D%3D2%5E%7B2%7D%5Ctimes3%3D12

2.辗转相除法

辗转相除法又称欧几里得算法(Euclidean algorithm)描述为:

eq?%5Cbegin%7Baligned%7D%20%5Cgcd%7B%5Cleft%28%5Calpha%2C0%20%5Cright%20%29%7D%26%3D%5Calpha%5C%5C%20%5Cgcd%7B%5Cleft%28%5Calpha%2C%20%5Cbeta%20%5Cright%20%29%7D%26%3D%5Cgcd%7B%5Cleft%28%5Calpha%2C%5Calpha%5Cmod%5Cbeta%20%5Cright%29%7D%5C%5C%20%5Calpha%5Cmod%5Cbeta%26%3D%5Calpha-%5Cbeta%5Ctimes%5Cleft%20%5Clfloor%20%5Cfrac%7B%5Calpha%7D%7B%5Cbeta%7D%20%5Cright%20%5Crfloor%20%5Cend%7Baligned%7D

求证辗转相除法

方法一

eq?%5Cbegin%7Barray%7D%7Brl%7D%20set%5C%20%5Calpha%26%3D%5Ckappa%5Ctimes%20%5Cbeta+%5Cgamma%5C%20%7B%5Cleft%28%7B%5Cleft%5C%7B%5Calpha%2C%5Cbeta%2C%5Ckappa%2C%5Cgamma%20%5Cright%5C%7D%7D%5Cin%20%5Cmathbb%7BN%7D%5E%7B+%7D%2C%5Calpha%3E%5Cbeta%2C%5Cbeta%3E%5Cgamma%20%5Cright%20%29%7D%5C%5C%20%5Cgamma%26%3D%5Calpha%5Cmod%5Cbeta%20%5Cend%7Barray%7D

eq?%7B%5Cleft%28%5Calpha%2C%5Cbeta%20%5Cright%29%7D的公约数eq?%5Cdelta

eq?%5Cbegin%7Baligned%7D%5Ctherefore%20%5Cgamma%26%3D%5Calpha-%5Ckappa%5Ctimes%5Cbeta%5CRightarrow%5Cfrac%7B%5Cgamma%7D%7B%5Cdelta%7D%3D%5Cfrac%7B%5Calpha%7D%7B%5Cdelta%7D-%5Ckappa%5Ctimes%5Cfrac%7B%5Cbeta%7D%7B%5Cdelta%7D%20%5Cend%7Baligned%7D

因为eq?%5Cdeltaeq?%5Calpha%2C%5Cbeta的约数。

eq?%7B%5Cbegin%7Baligned%7D%20%5Ctherefore%20%7B%5Cleft%5C%7B%5Cfrac%7B%5Calpha%7D%7B%5Cdelta%7D%2C%5Cfrac%7B%5Cbeta%7D%7B%5Cdelta%7D%5Cright%5C%7D%7D%5Cin%5Cmathbb%7BN%7D%5E%7B+%7D%20%5Cend%7Baligned%7D%7D

eq?%5Cbegin%7Baligned%7D%20%5Cbecause%26%20%5Ckappa%5Cin%5Cmathbb%7BN%7D%5E%7B+%7D%5C%5C%20%5Ctherefore%26%20%5Cfrac%7B%5Calpha%7D%7B%5Cdelta%7D-%5Ckappa%5Ctimes%5Cfrac%7B%5Cbeta%7D%7B%5Cdelta%7D%3D%5Cfrac%7B%5Cgamma%7D%7B%5Cdelta%7D%5Cin%5Cmathbb%7BN%7D%5E%7B+%7D%5C%5C%20%5Cend%7Baligned%7D

因此eq?%5Cdeltaeq?%5Cbetaeq?%5Cgamma%3D%5Calpha%5Cmod%5Cbeta的公约数。

因为eq?%5Cdelta原本就是eq?%7B%5Cleft%28%5Calpha%2C%5Cbeta%20%5Cright%29%7D的公约数,所以eq?%7B%5Cleft%28%5Calpha%2C%5Cbeta%20%5Cright%29%7Deq?%7B%5Cleft%28%5Cbeta%2C%5Calpha%5Cmod%5Cbeta%20%5Cright%20%29%7D有一样的约数。

因此eq?%5Cgcd%7B%5Cleft%28%5Calpha%2C%5Cbeta%20%5Cright%20%29%7D%3D%5Cgcd%7B%5Cleft%28%5Cbeta%2C%5Calpha%5Cmod%5Cbeta%20%5Cright%20%29%7D

方法二

{\left\{\alpha,\beta,\delta,\kappa \right\}}\in\mathbb{N}^{+},\alpha\geq \beta并且\gcd{​{\left(\alpha,\beta \right )}}=\delta\alpha\mod\beta=\alpha-\kappa\times\beta

\begin{aligned} \because& \frac{\alpha}{\delta}-\frac{\beta}{\delta}=\frac{\alpha-\beta}{\delta}\\ \therefore& \gcd{​{\left(\alpha,\beta \right )}}=\gcd{​{\left(\beta,\alpha-\beta \right )}} \end{aligned}

如果\alpha-\beta\geq \beta\gcd{​{\left(\alpha-\beta,\beta \right )}}=\gcd{​{\left(\beta,\alpha-2\times\beta \right )}}

如果\alpha-2\times\beta\geq \beta\gcd{​{\left(\alpha-2\times\beta,\beta \right )}}=\gcd{​{\left(\beta,\alpha-3\times\beta \right )}}

如果\alpha-{\left(\kappa-1 \right )}\times\beta\geq \beta\gcd{​{\left(\alpha-{\left(\kappa-1 \right )}\times\beta,\beta \right )}}=\gcd{​{\left(\beta, \alpha-\kappa\times\beta \right)}}

\begin{aligned} \because&\alpha\mod\beta=\alpha-\kappa\times\beta\\ \therefore&\gcd{​{\left(\alpha,\beta \right )}}=\gcd{​{\left(\beta, \alpha\mod\ \beta \right )}} \end{aligned}

辗转相除法案例

\begin{aligned} \gcd{​{\left(180,48 \right )}}&=\gcd{​{\left(48,180\mod48 \right )}}\\ &=\gcd{​{\left(48,36 \right)}}\\ &=\gcd{​{\left(36 ,48\mod36\right )}}\\ &=\gcd{​{\left(36,12 \right)}}\\ &=\gcd{​{\left(12, 36\mod12 \right )}}\\ &=\gcd{​{\left(12,0 \right )}}=12 \end{aligned}

c语言实现

/**
 * Copyright[2024] <Mopoke>
 */
#include <stdio.h>
#include <stdlib.h>

int gcd(int num_1, int num_2) {
    if (num_1 == 0) { return num_2; }
    if (num_2 == 0) { return num_1; }

    while ((num_1 %= num_2) && (num_2 %= num_1)) {}

    return num_1 + num_2;
}

int main(int argc, char* argv[]) {
    int num_1 = 0;
    int num_2 = 0;

    printf("Please input 2 positive integers:");
    scanf("%d %d", &num_1, &num_2);

    if (num_1 < 0 || num_2 < 0) {
        fprintf(stderr, "Error - Please input positive integers.\n");
        exit(EXIT_FAILURE);
    }

    printf("gcd(%d, %d) = %d\n", num_1, num_2, gcd(num_1, num_2));

    return EXIT_SUCCESS;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值