Skip to content

Commit 7ef2e4d

Browse files
authored
Add Project Euler Problem 092 (TheAlgorithms#5091)
* adde solution to problem 092 * added solution to problem 092 * fixed the pre-comit shebang issue
1 parent 8dc7cdb commit 7ef2e4d

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

project_euler/problem_092/__init__.py

Whitespace-only changes.

project_euler/problem_092/sol1.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
Project Euler Problem 092: https://projecteuler.net/problem=92
3+
Square digit chains
4+
A number chain is created by continuously adding the square of the digits in
5+
a number to form a new number until it has been seen before.
6+
For example,
7+
44 → 32 → 13 → 10 → 1 → 1
8+
85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89
9+
Therefore any chain that arrives at 1 or 89 will become stuck in an endless loop.
10+
What is most amazing is that EVERY starting number will eventually arrive at 1 or 89.
11+
How many starting numbers below ten million will arrive at 89?
12+
"""
13+
14+
15+
def next_number(number: int) -> int:
16+
"""
17+
Returns the next number of the chain by adding the square of each digit
18+
to form a neww number.
19+
For example if number = 12, next_number() will return 1^2 + 2^2 = 5.
20+
Therefore 5 is the next number of the chain.
21+
>>> next_number(44)
22+
32
23+
>>> next_number(10)
24+
1
25+
>>> next_number(32)
26+
13
27+
"""
28+
num = 0
29+
for i in range(len(str(number))):
30+
num += int(str(number)[i]) ** 2
31+
32+
return num
33+
34+
35+
def chain(number: int) -> bool:
36+
"""
37+
Generates the chain of numbers until the nest number generated is 1 0r 89.
38+
for example, if starting number is 44, then the function generates the
39+
following chain of numbers.
40+
chain: 44 → 32 → 13 → 10 → 1 → 1
41+
once the next number generated is 1 or 89, the function
42+
Returns True if the next number generated by next_number() if 1.
43+
Returns False if the next number generated by next_number() is 89.
44+
>>> chain(10)
45+
True
46+
>>> chain(58)
47+
False
48+
>>> chain(1)
49+
True
50+
"""
51+
while number != 1 and number != 89:
52+
number = next_number(number)
53+
54+
if number == 1:
55+
return True
56+
57+
elif number == 89:
58+
return False
59+
60+
61+
def solution(number: int = 10000000) -> int:
62+
"""
63+
The function returns the total numbers that end up in 89 after the chain generation.
64+
The function accepts a range number and the function checks all the values
65+
under value number.
66+
if the chain generation leads to the end number as 1 or 89. If the chain()
67+
returns True, then total is incremented, implying that the number we
68+
started with ended up with 1 else total2 is incremented, implying that
69+
the number we started with ended up in 89 after chain generation.
70+
But the function returns total2 as the requirement of question is
71+
to find out how many ended up in 89.
72+
73+
>>> solution(100)
74+
80
75+
>>> solution(10000000)
76+
8581146
77+
"""
78+
total = 0
79+
total2 = 0
80+
for i in range(1, number):
81+
val = chain(i)
82+
83+
if val is True:
84+
total += 1
85+
86+
elif val is False:
87+
total2 += 1
88+
89+
return total2
90+
91+
92+
if __name__ == "__main__":
93+
print(f"{solution() = }")

0 commit comments

Comments
 (0)