This is my code.  All comments welcome.  Last time run it only took 335 milliseconds:static bool CheckMultiples(int val){    for (int i = 1; i <= 20; i++)    {        if (val % i != 0)            return false;    }    return true;}static void Main(string[] args){    Stopwatch s = new Stopwatch();    s.Start();    int Num = 20;    while (!CheckMultiples(Num))    {        Num += 20;    }    s.Stop();    Console.WriteLine(The smallest number divisible by all numbers 1-20 is {0}., Num);    Console.WriteLine(The time took is {0} milliseconds., s.ElapsedMilliseconds);}
Project Euler #5 - Lowest common multiple of 1 through 20
c#;algorithm;programming challenge;mathematics
There is a far more efficient approach to finding the least common multiple (\$\operatorname{lcm}\$) of a set of integers which, unlike your and rolfl's code, involves no trial-and-error. Recall that for any pair \$(a, b)\$ of natural numbers, we have$$\operatorname{lcm}(a, b) = \frac{ab}{\operatorname{gcd}(a, b)}.$$This reduces the implementation of \$\operatorname{lcm}\$ to the implementation of \$\operatorname{gcd}\$, which is easily done using the simple and efficient Euclidean algorithm. Then, calculating the least common multiple of \$\{1, 2, ..., 20\}\$ is as simple as \$\operatorname{reduce}(\operatorname{lcm}, \{1, 2, ..., 20\})\$.You can find a simple Python 3 implementation of this idea in ideone here. Even in a slow interpreted language like Python, this is several orders of magnitude faster than your or rolfl's code, running in about .04 milliseconds.