Project Euler – Problem 5


What is the smallest number divisible by each of the numbers 1 to 20?

Problem:
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 number that is evenly divisible by all of the numbers from 1 to 20?

My Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Euler
{
    class Problem5 : IProblemBase
    {

        //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 number that is evenly divisible by all of the numbers from 1 to 20?

        public Problem5()
        {
        }

        public string GetAnswer()
        {
            bool foundThemAll = false;
            int smallestNumberDivisible = 0;

            while (foundThemAll == false)
            {
                bool foundThemAllInner = true;
                smallestNumberDivisible++;

                for (int i = 1; i <= 20; i++)
                {
                    if (smallestNumberDivisible % i != 0)
                    {
                        foundThemAllInner = false;
                        break;
                    }
                }
                if (foundThemAllInner)
                {
                    foundThemAll = true;
                }
            }

            return smallestNumberDivisible.ToString();
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: