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

相关文章:

  • 2022-03-04
  • 2021-06-08
  • 2021-05-29
  • 2021-08-24
  • 2022-12-23
  • 2021-09-16
  • 2022-02-07
  • 2022-12-23
猜你喜欢
  • 2022-02-15
  • 2022-12-23
  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-02
相关资源
相似解决方案