Skip to content

Commit 4bd5494

Browse files
VinWarepoyea
andauthored
Add solution to Problem 145 of Project Euler (#5464)
* Solution to Problem 145 of Project Euler * Provided more descriptive filename * Update sol1.py Co-authored-by: John Law <[email protected]>
1 parent 7a39441 commit 4bd5494

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

project_euler/problem_145/__init__.py

Whitespace-only changes.

project_euler/problem_145/sol1.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Project Euler problem 145: https://projecteuler.net/problem=145
3+
Author: Vineet Rao
4+
Problem statement:
5+
6+
Some positive integers n have the property that the sum [ n + reverse(n) ]
7+
consists entirely of odd (decimal) digits.
8+
For instance, 36 + 63 = 99 and 409 + 904 = 1313.
9+
We will call such numbers reversible; so 36, 63, 409, and 904 are reversible.
10+
Leading zeroes are not allowed in either n or reverse(n).
11+
12+
There are 120 reversible numbers below one-thousand.
13+
14+
How many reversible numbers are there below one-billion (10^9)?
15+
"""
16+
17+
18+
def odd_digits(num: int) -> bool:
19+
"""
20+
Check if the number passed as argument has only odd digits.
21+
>>> odd_digits(123)
22+
False
23+
>>> odd_digits(135797531)
24+
True
25+
"""
26+
num_str = str(num)
27+
for i in ["0", "2", "4", "6", "8"]:
28+
if i in num_str:
29+
return False
30+
return True
31+
32+
33+
def solution(max_num: int = 1_000_000_000) -> int:
34+
"""
35+
To evaluate the solution, use solution()
36+
>>> solution(1000)
37+
120
38+
>>> solution(1_000_000)
39+
18720
40+
>>> solution(10_000_000)
41+
68720
42+
"""
43+
result = 0
44+
# All single digit numbers reverse to themselves, so their sums are even
45+
# Therefore at least one digit in their sum is even
46+
# Last digit cannot be 0, else it causes leading zeros in reverse
47+
for num in range(11, max_num):
48+
if num % 10 == 0:
49+
continue
50+
num_sum = num + int(str(num)[::-1])
51+
num_is_reversible = odd_digits(num_sum)
52+
result += 1 if num_is_reversible else 0
53+
return result
54+
55+
56+
if __name__ == "__main__":
57+
print(f"{solution() = }")

0 commit comments

Comments
 (0)