[C ++] __ gcd (x, y) function

Accidentally discovered a useful function

__gcd (x, y) function

Used to find x, y greatest common divisor. x, y is not floating
header file: #include <algorithm>

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int a,b;
	cin>>a>>b;
	cout<<__gcd(a,b)<<endl;
}

Other methods of seeking the greatest common divisor

1 cycle

int gcd(int x,int y)
{
    int r;
	while (a%b!=0)
    {
        r=a%b;
        a=b;
        b=r;    
    }
    return b; 
}
int gcd(int a,int b) {
    return b>0 ? gcd(b,a%b):a;
}

2, recursive

Seeking the greatest common divisor of x and y, it is the greatest common divisor of y and x% y

int gcd(int a,int b)
{
	if(a%b==0) 
		return b;
	else 
		return (gcd(b,a%b));
}
Released eight original articles · won praise 0 · Views 150

Guess you like

Origin blog.csdn.net/qq_44455289/article/details/104071892
gcd