Skip to content

Commit c45fb3c

Browse files
MaximSmolskiygithub-actions
and
github-actions
authored
perf: Project Euler problem 145 solution 1 (TheAlgorithms#6259)
Improve solution (~30 times - from 900+ seconds to ~30 seconds) Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent b3d9281 commit c45fb3c

File tree

2 files changed

+67
-26
lines changed

2 files changed

+67
-26
lines changed

DIRECTORY.md

+3
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@
444444
* [Scoring Functions](machine_learning/scoring_functions.py)
445445
* [Sequential Minimum Optimization](machine_learning/sequential_minimum_optimization.py)
446446
* [Similarity Search](machine_learning/similarity_search.py)
447+
* [Support Vector Machines](machine_learning/support_vector_machines.py)
447448
* [Word Frequency Functions](machine_learning/word_frequency_functions.py)
448449

449450
## Maths
@@ -500,6 +501,7 @@
500501
* [Gaussian](maths/gaussian.py)
501502
* [Greatest Common Divisor](maths/greatest_common_divisor.py)
502503
* [Greedy Coin Change](maths/greedy_coin_change.py)
504+
* [Hamming Numbers](maths/hamming_numbers.py)
503505
* [Hardy Ramanujanalgo](maths/hardy_ramanujanalgo.py)
504506
* [Integration By Simpson Approx](maths/integration_by_simpson_approx.py)
505507
* [Is Ip V4 Address Valid](maths/is_ip_v4_address_valid.py)
@@ -938,6 +940,7 @@
938940
* [Bogo Sort](sorts/bogo_sort.py)
939941
* [Bubble Sort](sorts/bubble_sort.py)
940942
* [Bucket Sort](sorts/bucket_sort.py)
943+
* [Circle Sort](sorts/circle_sort.py)
941944
* [Cocktail Shaker Sort](sorts/cocktail_shaker_sort.py)
942945
* [Comb Sort](sorts/comb_sort.py)
943946
* [Counting Sort](sorts/counting_sort.py)

project_euler/problem_145/sol1.py

+64-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Project Euler problem 145: https://projecteuler.net/problem=145
3-
Author: Vineet Rao
3+
Author: Vineet Rao, Maxim Smolskiy
44
Problem statement:
55
66
Some positive integers n have the property that the sum [ n + reverse(n) ]
@@ -13,44 +13,82 @@
1313
1414
How many reversible numbers are there below one-billion (10^9)?
1515
"""
16+
EVEN_DIGITS = [0, 2, 4, 6, 8]
17+
ODD_DIGITS = [1, 3, 5, 7, 9]
1618

1719

18-
def odd_digits(num: int) -> bool:
20+
def reversible_numbers(
21+
remaining_length: int, remainder: int, digits: list[int], length: int
22+
) -> int:
1923
"""
20-
Check if the number passed as argument has only odd digits.
21-
>>> odd_digits(123)
22-
False
23-
>>> odd_digits(135797531)
24-
True
24+
Count the number of reversible numbers of given length.
25+
Iterate over possible digits considering parity of current sum remainder.
26+
>>> reversible_numbers(1, 0, [0], 1)
27+
0
28+
>>> reversible_numbers(2, 0, [0] * 2, 2)
29+
20
30+
>>> reversible_numbers(3, 0, [0] * 3, 3)
31+
100
2532
"""
26-
while num > 0:
27-
digit = num % 10
28-
if digit % 2 == 0:
29-
return False
30-
num //= 10
31-
return True
33+
if remaining_length == 0:
34+
if digits[0] == 0 or digits[-1] == 0:
35+
return 0
3236

37+
for i in range(length // 2 - 1, -1, -1):
38+
remainder += digits[i] + digits[length - i - 1]
3339

34-
def solution(max_num: int = 1_000_000_000) -> int:
40+
if remainder % 2 == 0:
41+
return 0
42+
43+
remainder //= 10
44+
45+
return 1
46+
47+
if remaining_length == 1:
48+
if remainder % 2 == 0:
49+
return 0
50+
51+
result = 0
52+
for digit in range(10):
53+
digits[length // 2] = digit
54+
result += reversible_numbers(
55+
0, (remainder + 2 * digit) // 10, digits, length
56+
)
57+
return result
58+
59+
result = 0
60+
for digit1 in range(10):
61+
digits[(length + remaining_length) // 2 - 1] = digit1
62+
63+
if (remainder + digit1) % 2 == 0:
64+
other_parity_digits = ODD_DIGITS
65+
else:
66+
other_parity_digits = EVEN_DIGITS
67+
68+
for digit2 in other_parity_digits:
69+
digits[(length - remaining_length) // 2] = digit2
70+
result += reversible_numbers(
71+
remaining_length - 2,
72+
(remainder + digit1 + digit2) // 10,
73+
digits,
74+
length,
75+
)
76+
return result
77+
78+
79+
def solution(max_power: int = 9) -> int:
3580
"""
3681
To evaluate the solution, use solution()
37-
>>> solution(1000)
82+
>>> solution(3)
3883
120
39-
>>> solution(1_000_000)
84+
>>> solution(6)
4085
18720
41-
>>> solution(10_000_000)
86+
>>> solution(7)
4287
68720
4388
"""
4489
result = 0
45-
# All single digit numbers reverse to themselves, so their sums are even
46-
# Therefore at least one digit in their sum is even
47-
# Last digit cannot be 0, else it causes leading zeros in reverse
48-
for num in range(11, max_num):
49-
if num % 10 == 0:
50-
continue
51-
num_sum = num + int(str(num)[::-1])
52-
num_is_reversible = odd_digits(num_sum)
53-
result += 1 if num_is_reversible else 0
90+
for length in range(1, max_power + 1):
91+
result += reversible_numbers(length, 0, [0] * length, length)
5492
return result
5593

5694

0 commit comments

Comments
 (0)