Project Euler 5

 

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的最小公倍数。首先,把互质的几个数——3,5,7,11,13,17,19相乘,得到4849845,易证得该最小公约数一定是4849845的倍数。然后用遍历法,判断其是否能被1~20整除,3、5、7、11、13、17、19不用验证,2、4、5、6、7、8、9由于20、18、16、15、14、12、10的存在不用验证。所以只用验证剩下的就可以了。
程序:

void main()
{
    long i;
    for(i = 4849845;; i = i+4849845)
    {
        if(i%20 == 0)
            if(i%18 == 0)
                if(i%16 == 0)
                    if(i%15 == 0)
                        if(i%14 == 0)
                            if(i%12 == 0)
                                if(i%10 ==0)break;
    }
    printf("%d\n",i);
}
作者:michaelxi007 发表于2013-11-17 23:14:46 原文链接
阅读:17 评论:0 查看评论

posted on 2013-11-17 23:15  清水老和尚  阅读(205)  评论(0编辑  收藏  举报

导航