Zachary Stence

Problem 5: Smallest multiple

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?

I first solved this problem May 29th, 2015 in the summer after my junior year of highschool. I do not have the original code I wrote, but I have solved the problem again and included the code below.

I solved this problem by simply looping through numbers in a while loop and checking whether or not they are divisible by all numbers 1 to 20 in a function divisible as shown below in the code snippet. This code is fairly elementary but there is a more elegant solution I really love that doesn't involve any code.

The idea is to try to come up with the prime factorization of the number we wish to find. Since we know that the number must be divisible by all numbers 1-20, we can simply start from 1 and add factors until all are covered. I have shown the process in the table to the right complete with an explanation at each step. The final prime factorization we reach is 24·32·5·7·11·13·17·19 = 232792560.

Next number
to cover
Current prime
factorization
Explanation
1   Already divisible by 1 (trivial)
2 2 Multiply by 2
3 2·3 Multiply by 3
4 22·3 Multiply by 2
5 22·3·5 Multiply by 5
6   Already divisible by 6 (2·3)
7 22·3·5·7 Mutliply by 7
8 23·3·5 Multiply by 2
9 23·32·5 Multiply by 3
10   Already divisible by 10 (2·5)
11 23·32·5·11 Mutliply by 11
12   Already divisible by 12 (22·3)
13 23·32·5·11·13 Multiply by 13
14 23·32·5·7·11·13 Mutliply by 7 (14 = 2·7)
15   Alreaddy divisible by 15 (3·5)
16 24·32·5·7·11·13 Multiply by 2
17 24·32·5·7·11·13·17 Mutliply by 17
18   Already divisible by 18 (2·32)
19 24·32·5·7·11·13·17·19 Multiply by 19
20   Already divisible by 20 (22·5)
#include <iostream>

bool divisible(int x, int max) {
  for (int i = 2; i <= max; i++)
    if (x % i != 0) return false;
  return true;
}

int main() {
  int i = 0;
  bool cont;
  do {
    i++;
    cont = !divisible(i, 20);
  } while (cont);
  std::cout << i << std::endl;
}

Answer: 232792560