Skip to content

Commit

Permalink
Add solution for problem 36.
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzDiebold committed Oct 21, 2019
1 parent 83ab650 commit c9a1200
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
14 changes: 14 additions & 0 deletions src/common/palindromes.py
Original file line number Diff line number Diff line change
@@ -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
9 changes: 1 addition & 8 deletions src/p004_largest_palindrome_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand Down
41 changes: 41 additions & 0 deletions src/p036_double_base_palindromes.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit c9a1200

Please sign in to comment.