您的位置:首页 > 其它

欧拉项目第五题 Smallest multiple

2016-03-09 11:12 429 查看
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly
divisible by all of the numbers from 1 to 20?

求能整除1-20的最小数。
public static void main(String[] args) {
        int a1 =2;
        int t =2;
        for(;a1<20;a1++){
            t = minMultiple(a1,t);
        }
        System.out.println(t);
    }
    
    //求最小公倍数
    static int minMultiple(int a, int b) {
        int r = a, s = a, t = b;
        if (a < b) {
            r = a;
            a = b;
            b = r;
        }
        while (r != 0) {
            r = a % b;
            a = b;
            b = r;
        }
        return s * t / a;
    }
这是循环处理,一个一个求最小公倍数运算的.
求公倍数运用了辗转相除法,这玩意我也忘了...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: