1717
1818def solution (max_power : int = 9 ) -> int :
1919 """
20- This solution counts reversible numbers below 10^max_power
21- using mathematical patterns instead of brute force.
20+ This solution counts reversible numbers below 10^max_power
21+ using mathematical patterns instead of brute force.
2222
23- A reversible number is a number where:
24- n + reverse(n)
23+ A reversible number is a number where:
24+ n + reverse(n)
2525
26- contains only odd digits.
26+ contains only odd digits.
2727
28- Example:
29- 36 + 63 = 99
30- 409 + 904 = 1313
28+ Example:
29+ 36 + 63 = 99
30+ 409 + 904 = 1313
3131
32- Instead of checking every number one by one, we observe
33- some repeating patterns based on the number of digits.
32+ Instead of checking every number one by one, we observe
33+ some repeating patterns based on the number of digits.
3434
35- --------------------------------------------------------
36- Main Observations
37- --------------------------------------------------------
35+ --------------------------------------------------------
36+ Main Observations
37+ --------------------------------------------------------
3838
39- 1. Numbers with length = 1 (mod 4)
40- ----------------------------------
41- These lengths never work because the carry pattern becomes
42- inconsistent while adding the number and its reverse.
39+ 1. Numbers with length = 1 (mod 4)
40+ ----------------------------------
41+ These lengths never work because the carry pattern becomes
42+ inconsistent while adding the number and its reverse.
4343
44- Examples:
45- 1 digit, 5 digits, 9 digits ...
44+ Examples:
45+ 1 digit, 5 digits, 9 digits ...
4646
47- Count = 0
47+ Count = 0
4848
4949
50- 2. Even length numbers
51- -----------------------
52- For numbers with even digits (2, 4, 6, 8 ...):
50+ 2. Even length numbers
51+ -----------------------
52+ For numbers with even digits (2, 4, 6, 8 ...):
5353
54- - Each pair of digits must produce an odd sum.
55- - One digit in the pair must be even and the other odd.
56- - The carry pattern stays consistent.
54+ - Each pair of digits must produce an odd sum.
55+ - One digit in the pair must be even and the other odd.
56+ - The carry pattern stays consistent.
5757
58- Counting possibilities:
59- - First pair has 20 valid combinations
60- (leading digit cannot be zero)
58+ Counting possibilities:
59+ - First pair has 20 valid combinations
60+ (leading digit cannot be zero)
6161
62- - Every inner pair has 30 valid combinations
62+ - Every inner pair has 30 valid combinations
6363
64- Formula:
65- 20 * 30^(k-1)
64+ Formula:
65+ 20 * 30^(k-1)
6666
67- where:
68- length = 2k
67+ where:
68+ length = 2k
6969
70- Examples:
71- 2 digits -> 20
72- 4 digits -> 600
73- 6 digits -> 18000
74- 8 digits -> 540000
70+ Examples:
71+ 2 digits -> 20
72+ 4 digits -> 600
73+ 6 digits -> 18000
74+ 8 digits -> 540000
7575
7676
77- 3. Length = 3 (mod 4)
78- ----------------------
79- These are lengths like:
80- 3, 7, 11 ...
77+ 3. Length = 3 (mod 4)
78+ ----------------------
79+ These are lengths like:
80+ 3, 7, 11 ...
8181
82- Here the middle digit creates a special carry cycle,
83- which only works for lengths of the form:
82+ Here the middle digit creates a special carry cycle,
83+ which only works for lengths of the form:
8484
85- 4j + 3
85+ 4j + 3
8686
87- Formula:
88- 100 * 500^j
87+ Formula:
88+ 100 * 500^j
8989
90- Examples:
91- 3 digits -> 100
92- 7 digits -> 50000
90+ Examples:
91+ 3 digits -> 100
92+ 7 digits -> 50000
9393
9494
95- --------------------------------------------------------
96- Complexity
97- --------------------------------------------------------
95+ --------------------------------------------------------
96+ Complexity
97+ --------------------------------------------------------
9898
99- Time Complexity:
100- O(max_power)
99+ Time Complexity:
100+ O(max_power)
101101
102- Space Complexity:
103- O(1)
102+ Space Complexity:
103+ O(1)
104104
105- The algorithm is extremely fast because it only loops
106- through digit lengths instead of checking every number.
107- """
105+ The algorithm is extremely fast because it only loops
106+ through digit lengths instead of checking every number.
107+ """
108108 result = 0
109109 for length in range (1 , max_power + 1 ):
110110 if length % 2 == 0 :
@@ -114,7 +114,7 @@ def solution(max_power: int = 9) -> int:
114114 elif length % 4 == 3 :
115115 # Odd length 4j+3 -> 100 x 500^j
116116 j = (length - 3 ) // 4
117- result += 100 * (500 ** j )
117+ result += 100 * (500 ** j )
118118 # Lengths == 1 (mod 4) contribute 0 and are intentionally skipped.
119119
120120 return result
@@ -132,4 +132,4 @@ def benchmark() -> None:
132132
133133if __name__ == "__main__" :
134134 print (f"Solution : { solution ()} " )
135- benchmark ()
135+ benchmark ()
0 commit comments