求解最大公因子(辗转相除法)

辗转相除法代码简化至9行
问题:给定两个数a,b,求它们的最大公因子

源代码:

public class Greater_Common_Divisor {
	public static void main(String args[]) {
		int a=9,b=15;
		System.out.println(a+" , "+b+"  ->  "+compute(a,b));
	}
	static int compute(int a,int b) {
		return a%b==0?b:compute(b,a%b);
	}
}

运行结果:
在这里插入图片描述

发布了53 篇原创文章 · 获赞 1 · 访问量 2773

猜你喜欢

转载自blog.csdn.net/weixin_43873198/article/details/105536369