1 | """ |
||
2 | Project Euler Problem 24: Lexicographic Permutations |
||
3 | ==================================================== |
||
4 | |||
5 | .. module:: solutions.problem24 |
||
6 | :synopsis: My solution to problem #24. |
||
7 | |||
8 | The source code for this problem can be |
||
9 | `found here <https://bitbucket.org/nekedome/project-euler/src/master/solutions/problem24.py>`_. |
||
10 | |||
11 | Problem Statement |
||
12 | ################# |
||
13 | |||
14 | A permutation is an ordered arrangement of objects. For example, :math:`3124` is one possible permutation of the digits |
||
0 ignored issues
–
show
|
|||
15 | :math:`1, 2, 3` and :math:`4`. If all of the permutations are listed numerically or alphabetically, we call it |
||
0 ignored issues
–
show
|
|||
16 | lexicographic order. The lexicographic permutations of :math:`0, 1` and :math:`2` are: |
||
17 | |||
18 | .. math:: |
||
19 | |||
20 | 012 \\mbox{ } 021 \\mbox{ } 102 \\mbox{ } 120 \\mbox{ } 201 \\mbox{ } 210 |
||
21 | |||
22 | What is the millionth lexicographic permutation of the digits :math:`0, 1, 2, 3, 4, 5, 6, 7, 8` and :math:`9`? |
||
0 ignored issues
–
show
|
|||
23 | |||
24 | Solution Discussion |
||
25 | ################### |
||
26 | |||
27 | I do not see an obvious closed-form solution to this problem, I will find the answer computationally. Python provides |
||
0 ignored issues
–
show
|
|||
28 | powerful permuted iterators that make this task very simply. The ``itertools`` module will be used to simply iterate |
||
0 ignored issues
–
show
|
|||
29 | over the permutations of the digits :math:`0` through :math:`9` until the millionth element is reached. The digits in |
||
0 ignored issues
–
show
|
|||
30 | this element will then be translated into a big-endian integer, this corresponds to the millionth number in the |
||
0 ignored issues
–
show
|
|||
31 | sequence. |
||
32 | |||
33 | Solution Implementation |
||
34 | ####################### |
||
35 | |||
36 | .. literalinclude:: ../../solutions/problem24.py |
||
37 | :language: python |
||
38 | :lines: 41- |
||
39 | """ |
||
40 | |||
41 | from itertools import islice, permutations |
||
42 | |||
43 | |||
44 | def solve(): |
||
45 | """ Compute the answer to Project Euler's problem #24 """ |
||
46 | target = 1000000 |
||
47 | range_limit = 10 |
||
48 | digit_permutations = permutations(range(range_limit)) |
||
49 | digits = next(islice(digit_permutations, target - 1, target)) |
||
50 | answer = sum([digit * 10 ** (range_limit - 1 - i) for i, digit in enumerate(digits)]) |
||
51 | return answer |
||
52 | |||
53 | |||
54 | expected_answer = 2783915460 |
||
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. ![]() |
|||
55 |
This check looks for lines that are too long. You can specify the maximum line length.