[Algorithm] Euclid's algorithm-solving the greatest common divisor

Idea:
If q is 0, then the greatest common divisor is p.
Otherwise, divide p by q to get the remainder r. The greatest common divisor of p and q is the greatest common divisor of q and r.

Code:

pubic static int gcd(int p, int q){
    
    
	if(q == 0) return p;
	int r = p % q;
	return gcd(q, r);
}

Guess you like

Origin blog.csdn.net/weixin_42020386/article/details/106708787