算法合集(1)

1.求两个数的最大公约数

    public static int resolve(int a, int b) {
        int t, r;
        if (a < b) {
            t = a;
            a = b;
            b = t;
        }
        while(b!=0){
            r = a%b;
            a = b;
            b = r;
        }
        return a;
    }

2.求正整数n以内的全部素数

    public static void resolve(int n) {
        int i,j,k;
        for(i=2;iint)Math.sqrt(i);
            for(j=2;j<=k;j++){
                if(i%j==0) break;
            }
            if(j>k){
                System.out.print(i+" ");
            } 
        }
    }

你可能感兴趣的:(java)