| 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 |
||
|
0 ignored issues
–
show
|
|||
| 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`? |
||
|
0 ignored issues
–
show
|
|||
| 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 |
||
|
0 ignored issues
–
show
|
|||
| 23 | :math:`2520` is also divisible by :math:`1` through :math:`10`. Any non-multiple will not be divisible by ALL numbers |
||
|
0 ignored issues
–
show
|
|||
| 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 |
||
|
0 ignored issues
–
show
|
|||
| 27 | (:math:`11`) since higher divisors will rule more candidates out by in-divisibility. More explicitly, :math:`19` out of |
||
|
0 ignored issues
–
show
|
|||
| 28 | every :math:`20` integers are not divisible by :math:`20` whereas only :math:`18` out of every :math:`19` integers are |
||
|
0 ignored issues
–
show
|
|||
| 29 | not divisible by :math:`19`. This is akin to lazy boolean logic evaluation and avoids redundant computation. |
||
|
0 ignored issues
–
show
|
|||
| 30 | |||
| 31 | Using these insights, a simple search strategy will find the answer very quickly. More specifically, search through |
||
|
0 ignored issues
–
show
|
|||
| 32 | increasing multiples of :math:`2520` testing for divisibility by :math:`20,19,\\dots,11` - in that order. Identify the |
||
|
0 ignored issues
–
show
|
|||
| 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: |
||
|
0 ignored issues
–
show
The name
n does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).
This check looks for invalid names for a range of different identifiers. You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements. If your project includes a Pylint configuration file, the settings contained in that file take precedence. To find out more about Pylint, please refer to their site. Loading history...
|
|||
| 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: |
||
|
0 ignored issues
–
show
The name
m does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).
This check looks for invalid names for a range of different identifiers. You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements. If your project includes a Pylint configuration file, the settings contained in that file take precedence. To find out more about Pylint, please refer to their site. Loading history...
|
|||
| 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 |
||
|
0 ignored issues
–
show
The name
n does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).
This check looks for invalid names for a range of different identifiers. You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements. If your project includes a Pylint configuration file, the settings contained in that file take precedence. To find out more about Pylint, please refer to their site. Loading history...
|
|||
| 64 | while not is_multiple(n, divisors): |
||
| 65 | n += 2520 # increment our search by 2520 at a time |
||
|
0 ignored issues
–
show
The name
n does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).
This check looks for invalid names for a range of different identifiers. You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements. If your project includes a Pylint configuration file, the settings contained in that file take precedence. To find out more about Pylint, please refer to their site. Loading history...
|
|||
| 66 | return n |
||
| 67 | |||
| 68 | |||
| 69 | expected_answer = 232792560 |
||
|
0 ignored issues
–
show
The name
expected_answer does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).
This check looks for invalid names for a range of different identifiers. You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements. If your project includes a Pylint configuration file, the settings contained in that file take precedence. To find out more about Pylint, please refer to their site. Loading history...
|
|||
| 70 |
This check looks for lines that are too long. You can specify the maximum line length.