| Total Complexity | 2 |
| Total Lines | 50 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | """ |
||
| 2 | Project Euler Problem 36: Double-Base Palindromes |
||
| 3 | ================================================= |
||
| 4 | |||
| 5 | .. module:: solutions.problem36 |
||
| 6 | :synopsis: My solution to problem #36. |
||
| 7 | |||
| 8 | The source code for this problem can be |
||
| 9 | `found here <https://bitbucket.org/nekedome/project-euler/src/master/solutions/problem36.py>`_. |
||
| 10 | |||
| 11 | Problem Statement |
||
| 12 | ################# |
||
| 13 | |||
| 14 | The decimal number, :math:`585 = 10010010012` (binary), is palindromic in both bases. |
||
| 15 | |||
| 16 | Find the sum of all numbers, less than one million, which are palindromic in base :math:`10` and base :math:`2`. |
||
|
|
|||
| 17 | |||
| 18 | .. note:: that the palindromic number, in either base, may not include leading zeros. |
||
| 19 | |||
| 20 | Solution Discussion |
||
| 21 | ################### |
||
| 22 | |||
| 23 | This solution simply searches through the integer range and identifies values that are palindromic in bases :math:`2` |
||
| 24 | and :math:`10`. These values are summed to produce the answer. |
||
| 25 | |||
| 26 | The one clever component is to only consider odd numbers. Every even integer has :math:`0` as its least significant bit |
||
| 27 | which is not allowed for the most significant bit. Therefore no even number is a base :math:`2` palindrome. |
||
| 28 | |||
| 29 | Solution Implementation |
||
| 30 | ####################### |
||
| 31 | |||
| 32 | .. literalinclude:: ../../solutions/problem36.py |
||
| 33 | :language: python |
||
| 34 | :lines: 37- |
||
| 35 | """ |
||
| 36 | |||
| 37 | from lib.digital import is_palindrome |
||
| 38 | |||
| 39 | |||
| 40 | def solve(): |
||
| 41 | """ Compute the answer to Project Euler's problem #36 """ |
||
| 42 | upper_bound = 1000000 |
||
| 43 | answer = 0 |
||
| 44 | palindromes = filter(lambda n: is_palindrome(n, 10) and is_palindrome(n, 2), range(1, upper_bound, 2)) |
||
| 45 | answer += sum(palindromes) |
||
| 46 | return answer |
||
| 47 | |||
| 48 | |||
| 49 | expected_answer = 872187 |
||
| 50 |
This check looks for lines that are too long. You can specify the maximum line length.