diff --git a/README.md b/README.md index 0b295fb..d711934 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # [Project Euler](https://projecteuler.net) Solutions -[![solved: 35 problems](https://img.shields.io/badge/solved-35_problems-f93.svg)](./src) +[![solved: 36 problems](https://img.shields.io/badge/solved-36_problems-f93.svg)](./src) ![Python: 3.7](https://img.shields.io/badge/Python-3.7-3776ab.svg) [![Build Status](https://travis-ci.com/FranzDiebold/project-euler-solutions.svg?branch=master)](https://travis-ci.com/FranzDiebold/project-euler-solutions) [![license: MIT](https://img.shields.io/badge/license-MIT-brightgreen.svg)](./LICENSE.md) diff --git a/src/common/palindromes.py b/src/common/palindromes.py new file mode 100644 index 0000000..5696c3d --- /dev/null +++ b/src/common/palindromes.py @@ -0,0 +1,14 @@ +""" +Palindrome utility functions. +""" + +from typing import Union + + +def is_palindromic_number(number: Union[int, str]) -> bool: + """Check if a number is palindromic number.""" + number_str = str(number) + for i in range(len(number_str) // 2): + if number_str[i] != number_str[-1 * (i + 1)]: + return False + return True diff --git a/src/p004_largest_palindrome_product.py b/src/p004_largest_palindrome_product.py index 3091808..20bbbd8 100644 --- a/src/p004_largest_palindrome_product.py +++ b/src/p004_largest_palindrome_product.py @@ -11,14 +11,7 @@ from typing import Iterator, Tuple from heapq import heappush, heappop - -def is_palindromic_number(num: int) -> bool: - """Check if a number is palindromic number.""" - num_str = str(num) - for i in range(len(num_str) // 2): - if num_str[i] != num_str[-1 * (i + 1)]: - return False - return True +from src.common.palindromes import is_palindromic_number def _get_lower_diagonal_tuples(tuple_sum: int, max_value: int) -> Iterator[Tuple[int, int]]: diff --git a/src/p036_double_base_palindromes.py b/src/p036_double_base_palindromes.py new file mode 100644 index 0000000..34960ef --- /dev/null +++ b/src/p036_double_base_palindromes.py @@ -0,0 +1,41 @@ +""" +Problem 36: Double-base palindromes +http://projecteuler.net/problem=36 + +The decimal number, 585 = 1001001001 (binary), is palindromic in both bases. + +Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2. + +(Please note that the palindromic number, in either base, may not include leading zeros.) +""" + +from typing import Iterable + +from src.common.palindromes import is_palindromic_number + + +def _get_number_in_binary(number: int) -> str: + """Get the binary representation of a given decimal number `number` as string.""" + binary_representation = '' + while number > 0: + binary_representation = str(number % 2) + binary_representation + number //= 2 + return binary_representation + + +def _get_double_base_palindromes(threshold: int) -> Iterable[int]: + """Get numbers, which are paldindromic in base 10 and base 2.""" + for number in range(threshold): + if is_palindromic_number(number) and is_palindromic_number(_get_number_in_binary(number)): + yield number + + +def main() -> None: + """Main function.""" + threshold = int(1e6) + print(f'The sum of all numbers, less than {threshold:,}, which are palindromic ' \ + f'in base 10 and base 2 is {sum(_get_double_base_palindromes(threshold)):,}.') + + +if __name__ == '__main__': + main()