Thursday, September 3, 2009

Project Euler Problem 5

I'd like to post two solutions to this problem. The problem listed below took 2 hours and 20 minutes to run on my antiquated machine. Project Euler says optimized solutions take less than a minute on a modern machine. I can already see ways to optimize my solution, even though I got this one right on the first try.


/*
* 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 class PE0005
{
public static void main(String[] args)
{
long currentNumber = 0;
boolean foundAnswer = false;

while (!foundAnswer)
foundAnswer = checkDivisibility(++currentNumber) ? true : false;

System.out.println(currentNumber);
}

private static boolean checkDivisibility(long currentNumber)
{
for (int i = 2; i <= 20; i++)
if (currentNumber % i != 0)
return false;

return true;
}
}

No comments:

Post a Comment