求最大公约数

  1. 暴力枚举法
public class test {

    public static int genGreatestCommonDivisor(int a,int b){
        int big = a>b ? a:b;
        int small = a1; i++) {
            if(small %i==0 && big%i==0){
                return i;
            }
        }
        return  1;
    }


    public static void main(String[] args) {
        System.out.println(genGreatestCommonDivisor(25,5));
    }
}

  1. 欧几里得算法
public class test {

    public static int genGreatestCommonDivisor(int a,int b){
        int big = a>b ? a:b;
        int small = a
  1. 更相减损术
public class test {

    public static int genGreatestCommonDivisor(int a,int b){
       if(a == b){
           return a;
       }
       int big = a>b ? a:b;
       int small = a

你可能感兴趣的:(求最大公约数)