这道题目比较简单,需要注意的是,能被1到20同时整除的数,一定小于 1 到 20 的叠乘,一定包含 1 到 20 的所有素数,同时,对于每一个包含的素数x,如果有其 higher order term , N, 【Math.Power(x, N) < 20】,就不需要其 lower order terms. 比如 有了 2^4 = 16, 就不需要 2^3, 2^2, 2^1, 这些数了。
另外需要注意的是,C# 中,对于四舍五入的函数,Math.Round(), 的处理方式是,四舍六入五成双 的。 用 Ceiling和 Floor 方法来向上向下取整。
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectEuler01
{
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Start();
// Write your code here!
List
primels = new List
() { 2, 3, 5, 7,11,13,17,19 };
double maxlog;
double result = 1.0;
double mynum = 20;
foreach (int item in primels)
{
maxlog = Math.Log(mynum, item);
maxlog = Math.Floor(maxlog);
result = result * Math.Pow(item, maxlog);
}
Console.WriteLine(result+"是最小能被{0}整除的数",mynum);
watch.Stop();
double ts = watch.Elapsed.TotalMilliseconds;
Console.WriteLine("Totoal running time is " + ts + " ms ");
Console.ReadLine();
}
}
class MyClass
{
}
}