Java solution to Project Euler Problem 5

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

Running time: 125ms

Assessment: LOL.

public class Problem005
{
	public static void main(String[] args)
	{
		long begin = System.currentTimeMillis();
		int i = 20;
		while (true)
		{
			if (	(i % 1 == 0) &&
				(i % 2 == 0) &&
				(i % 3 == 0) &&
				(i % 4 == 0) &&
				(i % 5 == 0) &&
				(i % 6 == 0) &&
				(i % 7 == 0) &&
				(i % 8 == 0) &&
				(i % 9 == 0) &&
				(i % 10 == 0) &&
				(i % 11 == 0) &&
				(i % 12 == 0) &&
				(i % 13 == 0) &&
				(i % 14 == 0) &&
				(i % 15 == 0) &&
				(i % 16 == 0) &&
				(i % 17 == 0) &&
				(i % 18 == 0) &&
				(i % 19 == 0) &&
				(i % 20 == 0) )
			{
				break;
			}
			i += 20;
		}
		long end = System.currentTimeMillis();
		System.out.println(i);
		System.out.println(end-begin + "ms");
	}	
}

2 thoughts on “Java solution to Project Euler Problem 5

  1. And here is my solution which is easier to understand ;

    public static void main ( String []args)
    {
    int a = 2520;
    int b = 1;
    while (true)
    {
    if (a%b==0)
    {
    b++;
    if (b==20)
    {
    System.out.println(a);
    break;
    }
    }
    else
    {
    a++;
    b=1;
    }
    }

    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *