Where would you like to go, Toady? RSS 2.0
# Monday, February 09, 2009

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;
        }
    }
}
Monday, February 09, 2009 10:44:54 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
Bookmark, Tweet, or Share
Stackoverflow.com Rep
Flickr stream
Archive
<February 2009>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Chris Ballance
Sign In
Statistics
Total Posts: 46
This Year: 11
This Month: 2
This Week: 0
Comments: 11
All Content © 2010, Chris Ballance

Valid XHTML 1.0 Transitional