Euler Problem 5

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?

I’m thinking brute force. there’s probably something simpler using factorials, but this works albeit slowly. Took about 90 seconds on my loaded system.

public class Euler5
{
    public static void main(String[] args)
    {
        System.out.print(“Problem 5:\n”);
        Euler5 e = new Euler5();
        System.out.print(“Smallest number = ” + e.Problem5()+ “\n”);
    }
    public String Problem5 ()
    {
        int first = 0;
        for (int i = 20; i <= 300000000; i++)
        {
            boolean test = true;
            for (int n = 1; n <= 20; n++)
            {
                if (i % n != 0)
                {
                    test = false;
                }
            }
            if (test == true)
            {
                first = i;
                break;
            }
        }
        return String.valueOf(first);
    }
}

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 )

Connecting to %s