尔冬橙

博客园 首页 新随笔 联系 订阅 管理

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?

 static void Main(string[] args)
        {
            long startTime, endTime;
            Int64 f=2;
            int i, j,k;
            int[] a = new int[20];
            startTime = DateTime.Now.Ticks;
            for (i = 0; i < 20; i++)
            {
                a[i] = i + 1;
            }
            for (i = 2; i < 20; i++)
            {
                k = a[i];
                if (isPrime(a[i]) == false)
                {
                    for (j = 1; j <i; j++)
                    {
                        if (k % a[j] == 0) k /= a[j];
                    }
                    a[i] = k;
                }
                f *= k;
            }
            endTime = DateTime.Now.Ticks;
            Console.WriteLine("run time:{0}ms",(endTime-startTime)/10000.0);
            Console.WriteLine(f);
            Console.ReadLine();
        }
        public static bool isPrime(int n)
        {
            for (int i = 2; i <= Math.Sqrt(n); i++)
            {
                if (n % i == 0) return false;
            }
            return true;
        }

结果:232792560

posted on 2012-05-13 22:04  尔冬橙  阅读(293)  评论(0编辑  收藏  举报