This one was elusively easy since I went with a simple brute-force approach. Execution averages 1 second.
using System;
using System.Diagnostics;
namespace Euler5
{
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
int lcm = FindLeastCommonMultiple();
sw.Stop();
Console.WriteLine("Runtime was " + sw.ElapsedMilliseconds + " ms");
Console.WriteLine("Runtime was " + sw.ElapsedTicks + " ticks");
Console.WriteLine("LCM is " + lcm);
Console.ReadLine();
}
private static int FindLeastCommonMultiple()
{
for (int i = 2520; i < int.MaxValue; i += 20)
{
if (IsDivisibleByRange(i, 1, 20))
return i;
}
return -1;
}
static long SmallestNumber()
{
return factorial(20);
}
static long factorial(long n)
{
long returnValue = 1;
for (int i = 1; i < n; i++)
{
returnValue *= i;
}
return returnValue;
}
static bool IsDivisibleByRange(int num, int begin, int end)
{
for (int i = begin; i <= end; i++)
{
if ((num % i) != 0)
{
return false;
}
}
return true;
}
}
}