@@ -17,30 +17,30 @@ def next_number(number: int) -> int:
17
17
Returns the next number of the chain by adding the square of each digit
18
18
to form a neww number.
19
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.
20
+ Therefore, 5 is the next number of the chain.
21
21
>>> next_number(44)
22
22
32
23
23
>>> next_number(10)
24
24
1
25
25
>>> next_number(32)
26
26
13
27
27
"""
28
- num = 0
29
- for i in range (len (str (number ))):
30
- num += int (str (number )[i ]) ** 2
28
+ sum_of_digits_squared = 0
29
+ while number :
30
+ sum_of_digits_squared += (number % 10 ) ** 2
31
+ number //= 10
31
32
32
- return num
33
+ return sum_of_digits_squared
33
34
34
35
35
36
def chain (number : int ) -> bool :
36
37
"""
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.
38
+ The function generates the chain of numbers until the next number is 1 or 89.
39
+ For example, if starting number is 44, then the function generates the
40
+ following chain of numbers:
41
+ 44 → 32 → 13 → 10 → 1 → 1.
42
+ Once the next number generated is 1 or 89, the function returns whether
43
+ or not the the next number generated by next_number() is 1.
44
44
>>> chain(10)
45
45
True
46
46
>>> chain(58)
@@ -51,43 +51,26 @@ def chain(number: int) -> bool:
51
51
while number != 1 and number != 89 :
52
52
number = next_number (number )
53
53
54
- if number == 1 :
55
- return True
56
-
57
- elif number == 89 :
58
- return False
54
+ return number == 1
59
55
60
56
61
57
def solution (number : int = 10000000 ) -> int :
62
58
"""
63
- The function returns the total numbers that end up in 89 after the chain generation .
59
+ The function returns the number of integers that end up being 89 in each chain.
64
60
The function accepts a range number and the function checks all the values
65
61
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
62
73
63
>>> solution(100)
74
64
80
75
65
>>> solution(10000000)
76
66
8581146
77
67
"""
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
68
+ return sum (1 for i in range (1 , number ) if not chain (i ))
85
69
86
- elif val is False :
87
- total2 += 1
88
70
89
- return total2
71
+ if __name__ == "__main__" :
72
+ import doctest
90
73
74
+ doctest .testmod ()
91
75
92
- if __name__ == "__main__" :
93
76
print (f"{ solution () = } " )
0 commit comments