Kjell Vos Portfolio!

I post about code here and other IT related subject matter.

Project Euler Problem 5 Java

In this blog post we will we be looking at problem 5 of Project Euler and we will program a solution in Java.

The problem of Project Euler found here, below is the problem for quick lookup.

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?

-Project Euler problem 5

Let’s break the problem into some smaller ones:

  • We need to consider 11 to 20 for division.(we can skip 1-10 since it would result in the same whole number just twice as large)
  • We need to loop till we find a suitable number and then stop looping and print the result.

It’s actually quite a simple problem if you do not over engineer it.

We need a for loop and an array to hold the numbers to check with division. We start by setting the ‘step’ variable to 20(anything less than 20 will not be dividable by 20). After that we declare an integer array named ‘numbersToCheck’ and fill it with 11 to 20. Then we declare the first for loop, the count variable of the loop gets set to 20 at the start and the loop condition being as long as count is less than 1.000.000.000 it will continue the loop. Every loop we increment count with step(+20).

After that in the first for loop we create a boolean value called ‘flag’ which we set to true. Then we declare another for loop(the second) which loops over all the numbers to check which are held in the array. Inside the second for loop we check if the current count value is evenly dividable by the current number to check. We do this for every value in the array.

If any of the tried numbers do leave a remainder it sets flag to false and breaks out of the second for loop.

If all numbers are checked and none of then leave a remainder it exits the second loop after running through all options. After that the last if statement inside the first for loop checks if the flag boolean is still true, If it is it prints out the count and exits out of the first for loop.

Were the program never able to find a number below 1.000.000.000 dividable by 11 to 20 then it stops without printing a value.

int step = 20;
int[] numbersToCheck = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
for (int count = step; count < 1_000_000_000; count += step){
    boolean flag = true;
    for (int numberToCheck : numbersToCheck){
        if (count % numberToCheck != 0){
            flag = false;
            break;
        }
    }
    if (flag){
        System.out.println(count);
        break;
    }
}

Check out the ‘Project Euler in Java’ page for more solutions!

Link to github.

That was Project Euler problem 5 in Java!

Leave a Reply

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

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.