1
|
|
|
""" |
2
|
|
|
Project Euler Problem 5: Smallest Multiple |
3
|
|
|
========================================== |
4
|
|
|
|
5
|
|
|
.. module:: solutions.problem5 |
6
|
|
|
:synopsis: My solution to problem #5. |
7
|
|
|
|
8
|
|
|
The source code for this problem can be |
9
|
|
|
`found here <https://bitbucket.org/nekedome/project-euler/src/master/solutions/problem5.py>`_. |
10
|
|
|
|
11
|
|
|
Problem Statement |
12
|
|
|
################# |
13
|
|
|
|
14
|
|
|
:math:`2520` is the smallest number that can be divided by each of the numbers from :math:`1` to :math:`10` without any |
|
|
|
|
15
|
|
|
remainder. |
16
|
|
|
|
17
|
|
|
What is the smallest positive number that is evenly divisible by all of the numbers from :math:`1` to :math:`20`? |
|
|
|
|
18
|
|
|
|
19
|
|
|
Solution Discussion |
20
|
|
|
################### |
21
|
|
|
|
22
|
|
|
:math:`2520` is the smallest multiple of all numbers :math:`1` through :math:`10`, which means that any multiple of |
|
|
|
|
23
|
|
|
:math:`2520` is also divisible by :math:`1` through :math:`10`. Any non-multiple will not be divisible by ALL numbers |
|
|
|
|
24
|
|
|
:math:`1` through :math:`10`, so can be ignored. |
25
|
|
|
|
26
|
|
|
Divisibility by the numbers :math:`11` through :math:`20` should be tested from highest (:math:`20`) to lowest |
|
|
|
|
27
|
|
|
(:math:`11`) since higher divisors will rule more candidates out by in-divisibility. More explicitly, :math:`19` out of |
|
|
|
|
28
|
|
|
every :math:`20` integers are not divisible by :math:`20` whereas only :math:`18` out of every :math:`19` integers are |
|
|
|
|
29
|
|
|
not divisible by :math:`19`. This is akin to lazy boolean logic evaluation and avoids redundant computation. |
|
|
|
|
30
|
|
|
|
31
|
|
|
Using these insights, a simple search strategy will find the answer very quickly. More specifically, search through |
|
|
|
|
32
|
|
|
increasing multiples of :math:`2520` testing for divisibility by :math:`20,19,\\dots,11` - in that order. Identify the |
|
|
|
|
33
|
|
|
first, and thus smallest such number. |
34
|
|
|
|
35
|
|
|
Solution Implementation |
36
|
|
|
####################### |
37
|
|
|
|
38
|
|
|
.. literalinclude:: ../../solutions/problem5.py |
39
|
|
|
:language: python |
40
|
|
|
:lines: 43- |
41
|
|
|
""" |
42
|
|
|
|
43
|
|
|
from typing import List |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def is_multiple(n: int, divisors: List[int]) -> bool: |
|
|
|
|
47
|
|
|
""" Check whether :math:`n` is divisible by all of the given :math:`divisors` |
48
|
|
|
|
49
|
|
|
:param n: the integer to check for divisibility |
50
|
|
|
:param divisors: the divisors to test :math:`n` with |
51
|
|
|
:return: whether :math:`n` is divisible by all :math:`divisors` or not |
52
|
|
|
""" |
53
|
|
|
|
54
|
|
|
for m in divisors: |
|
|
|
|
55
|
|
|
if n % m != 0: |
56
|
|
|
return False |
57
|
|
|
return True |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
def solve(): |
61
|
|
|
""" Compute the answer to Project Euler's problem #5 """ |
62
|
|
|
divisors = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11] # reverse order |
63
|
|
|
n = 2520 # start our search at 2520 |
|
|
|
|
64
|
|
|
while not is_multiple(n, divisors): |
65
|
|
|
n += 2520 # increment our search by 2520 at a time |
|
|
|
|
66
|
|
|
return n |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
expected_answer = 232792560 |
|
|
|
|
70
|
|
|
|
This check looks for lines that are too long. You can specify the maximum line length.