Consolidation of knowledge points about the Euclid algorithm to calculate the greatest common divisor and the least common multiple algorithm

Introduce:

        I don't have a thorough understanding of the Euclidean algorithm to calculate the greatest common divisor, so I want to review the content by myself.

Learn from:

none

Detail:

 

Important:

 

Euclidean algorithm:

        1. Use the remainder to filter out the smaller number as the dividend

        2. Use continuous division to obtain the greatest common divisor

The algorithm of the greatest common multiple: the product of two numbers divided by the greatest common divisor is the least common multiple

EX:

#include<stdio.h>
int gcd(int m,int n){//欧几里得算法——最大公约数
    int res;//筛选较大值置于被除数位置
    while(n!=0){
        res=m%n;
        m=n;//除数被除数交换
        n=res;//
    }
    return m;
}
int lcm(int m,int n){
    int res;//两数乘积除以最大公约数为最小公倍数
    res=gcd(m,n);
    res=(m*n)/res;
    return res;
}
int main()
{
    int m,n;
    scanf("%d%d",&m,&n);
    printf("%d %d",gcd(m,n),lcm(m,n));
    return 0;
}

Summary:

Guess you like

Origin blog.csdn.net/Crabfishhhhh/article/details/128942475