Skip to content

Commit 84facb7

Browse files
Saksham1970github-actionscclauss
authored
Project Euler: 092 decreased the time (#6627)
* Added explanation and increased speed of the solution of problem 092 * updating DIRECTORY.md * Added temporary fix to the failing of problem 104 * Reduced few seconds by minor improvements * Update sol.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 00dfad9 commit 84facb7

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

project_euler/problem_092/sol1.py

+32-10
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
How many starting numbers below ten million will arrive at 89?
1212
"""
1313

14-
15-
DIGITS_SQUARED = [digit**2 for digit in range(10)]
14+
DIGITS_SQUARED = [sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(100000)]
1615

1716

1817
def next_number(number: int) -> int:
18+
1919
"""
2020
Returns the next number of the chain by adding the square of each digit
2121
to form a new number.
@@ -28,15 +28,29 @@ def next_number(number: int) -> int:
2828
>>> next_number(32)
2929
13
3030
"""
31+
3132
sum_of_digits_squared = 0
3233
while number:
33-
sum_of_digits_squared += DIGITS_SQUARED[number % 10]
34-
number //= 10
34+
35+
# Increased Speed Slightly by checking every 5 digits together.
36+
sum_of_digits_squared += DIGITS_SQUARED[number % 100000]
37+
number //= 100000
3538

3639
return sum_of_digits_squared
3740

3841

39-
CHAINS = {1: True, 58: False}
42+
# There are 2 Chains made,
43+
# One ends with 89 with the chain member 58 being the one which when declared first,
44+
# there will be the least number of iterations for all the members to be checked.
45+
46+
# The other one ends with 1 and has only one element 1.
47+
48+
# So 58 and 1 are chosen to be declared at the starting.
49+
50+
# Changed dictionary to an array to quicken the solution
51+
CHAINS: list[bool | None] = [None] * 10000000
52+
CHAINS[0] = True
53+
CHAINS[57] = False
4054

4155

4256
def chain(number: int) -> bool:
@@ -54,11 +68,16 @@ def chain(number: int) -> bool:
5468
>>> chain(1)
5569
True
5670
"""
57-
if number in CHAINS:
58-
return CHAINS[number]
71+
72+
if CHAINS[number - 1] is not None:
73+
return CHAINS[number - 1] # type: ignore
5974

6075
number_chain = chain(next_number(number))
61-
CHAINS[number] = number_chain
76+
CHAINS[number - 1] = number_chain
77+
78+
while number < 10000000:
79+
CHAINS[number - 1] = number_chain
80+
number *= 10
6281

6382
return number_chain
6483

@@ -74,12 +93,15 @@ def solution(number: int = 10000000) -> int:
7493
>>> solution(10000000)
7594
8581146
7695
"""
77-
return sum(1 for i in range(1, number) if not chain(i))
96+
for i in range(1, number):
97+
if CHAINS[i] is None:
98+
chain(i + 1)
99+
100+
return CHAINS[:number].count(False)
78101

79102

80103
if __name__ == "__main__":
81104
import doctest
82105

83106
doctest.testmod()
84-
85107
print(f"{solution() = }")

0 commit comments

Comments
 (0)