HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Smallest Multiple

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
smallestmultiplestackoverflow

Problem

I just recently learned about Project Euler and have started doing the problems on there. I cleared problem 1 and 2, had no idea how to do 3 and 4, and started to do 5. I've seen the post regarding the quick mathematic solution, but I'd like to know if there are better ways to do it programmatically.

For those that don't know, question #5 is as follows:

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?

The logic behind my algorithm is simple:

The number in question must be divisible by all of the numbers between 1 and 20 (assume inclusive for the ranges). As a result, it must be divisible by all of the numbers between 1 and 10. The smallest number that is divisible by all the numbers between 1 and 10 (given by Project Euler) is 2520. Thus, the number in question must be a multiple of 2520. (As I'm writing this, I'm starting to question whether or not this is a necessary truth, but the program does work. Don't really want to go about proving it right now. I'm fairly certain however.) Thus, I can start at 2520 + 2520 and simply add 2520 every iteration.

It is guaranteed that these multiples will be divisible by all the numbers between 1 and 10, so there is no need to check those numbers. So I start from 11 and go to 20.

public static long problem5() {
    long i = 2520;
    boolean found = false;
    while (!found) {
        i += 2520;
        boolean divis = true;
        for (int j = 11; j <= 20; j++) {
            if (i % j != 0) {
                divis = false;
                //System.out.println(i + " is not divisible by " + j);
                break;
            }
            else {
                //System.out.println(i + " is divisible by " + j);
            }
        }
        if (divis) {
            found = true;
        }
    }
    return i;
}


I made this quick program to check my math r

Solution

Maybe you can reason as follows:

Multiplying all the numbers together from 1->10 gives you 3628800 which is indeed divisible by the numbers 1->10, but it is not the minimum number. Ask yourself why? take the last number 10 for instance. Do we really need to multiply 10 into our answer 10, when we know that our number already has a factor of 2 and a factor of 5?

Consider the prime factors of the numbers 1->10

factors:

1 - 1
 2 - 2
 3 - 3
 4 - 2*2   (2^2)
 5 - 5
 6 - 2*3
 7 - 7
 8 - 2*2*2 (2^3)
 9 - 3*3   (3^2)
10 - 2*5


What's the most number of 2's you need to create any of the values 1->10...? 3 (8=2^3)

What's the most number of 3's? 2 (9=3^2)

What's the most number of 5's? 1 (5)

What's the most number of 7's? 1 (7)

Thus, the answer for the minimum number that is a multiple of 1->10, is 2^33^25*7 = 2520.

You can extend this logic for 1->20 and you'll get a much quicker algorithm than what you've proposed.

Code Snippets

1 - 1
 2 - 2
 3 - 3
 4 - 2*2   (2^2)
 5 - 5
 6 - 2*3
 7 - 7
 8 - 2*2*2 (2^3)
 9 - 3*3   (3^2)
10 - 2*5

Context

StackExchange Code Review Q#13086, answer score: 18

Revisions (0)

No revisions yet.