Skip to content

Commit 015c487

Browse files
authored
fix Doctests
1 parent 9f63385 commit 015c487

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

maths/perfect_cube.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
def perfect_cube(n: int) -> bool:
22
"""
33
Check if a number is a perfect cube or not.
4-
4+
55
Note: This method uses floating point arithmetic which may be
66
imprecise for very large numbers.
7-
7+
88
>>> perfect_cube(27)
99
True
1010
>>> perfect_cube(64)
@@ -30,12 +30,12 @@ def perfect_cube(n: int) -> bool:
3030
is_negative = True
3131
else:
3232
is_negative = False
33-
33+
3434
val = n ** (1 / 3)
3535
# Round to avoid floating point precision issues
3636
rounded_val = round(val)
3737
result = rounded_val * rounded_val * rounded_val == n
38-
38+
3939
# For negative numbers, we need to check if the cube root would be negative
4040
return result and not (is_negative and rounded_val == 0)
4141

@@ -45,7 +45,7 @@ def perfect_cube_binary_search(n: int) -> bool:
4545
Check if a number is a perfect cube or not using binary search.
4646
Time complexity : O(Log(n))
4747
Space complexity: O(1)
48-
48+
4949
>>> perfect_cube_binary_search(27)
5050
True
5151
>>> perfect_cube_binary_search(64)
@@ -70,7 +70,9 @@ def perfect_cube_binary_search(n: int) -> bool:
7070
False
7171
>>> perfect_cube_binary_search(10**100) # Extremely large number
7272
False
73-
>>> perfect_cube_binary_search(10**300) # Extremely large number
73+
>>> perfect_cube_binary_search(10**300) # Extremely large perfect cube (10^100)^3
74+
True
75+
>>> perfect_cube_binary_search(10**300 + 1) # Extremely large non-perfect cube
7476
False
7577
>>> perfect_cube_binary_search("a")
7678
Traceback (most recent call last):
@@ -91,29 +93,29 @@ def perfect_cube_binary_search(n: int) -> bool:
9193
"""
9294
if not isinstance(n, int):
9395
raise TypeError("perfect_cube_binary_search() only accepts integers")
94-
96+
9597
# Handle zero and negative numbers
9698
if n == 0:
9799
return True
98100
if n < 0:
99101
n = -n
100-
102+
101103
# Quick checks to eliminate obvious non-cubes
102104
# Check last three digits using modulo arithmetic
103105
# Only 0, 1, 8, 7, 4, 5, 6, 3, 2, 9 can be cubes mod 10
104106
# But for cubes, the pattern is more complex
105107
last_digit = n % 10
106108
if last_digit not in {0, 1, 8, 7, 4, 5, 6, 3, 2, 9}:
107109
return False
108-
110+
109111
# More refined check: cubes mod 7 can only be 0, 1, 6
110112
if n % 7 not in {0, 1, 6}:
111113
return False
112-
114+
113115
# More refined check: cubes mod 9 can only be 0, 1, 8
114116
if n % 9 not in {0, 1, 8}:
115117
return False
116-
118+
117119
# Estimate the cube root using logarithms for very large numbers
118120
# This gives us a much better initial right bound
119121
if n > 10**18:
@@ -126,7 +128,7 @@ def perfect_cube_binary_search(n: int) -> bool:
126128
else:
127129
# For smaller numbers, use the standard approach
128130
left, right = 0, n // 2 + 1
129-
131+
130132
# Binary search
131133
while left <= right:
132134
mid = (left + right) // 2
@@ -135,19 +137,19 @@ def perfect_cube_binary_search(n: int) -> bool:
135137
if mid > 10**6 and mid * mid > n // mid:
136138
right = mid - 1
137139
continue
138-
140+
139141
cube = mid * mid * mid
140142
if cube == n:
141143
return True
142144
elif cube < n:
143145
left = mid + 1
144146
else:
145147
right = mid - 1
146-
148+
147149
return False
148150

149151

150152
if __name__ == "__main__":
151153
import doctest
152154

153-
doctest.testmod()
155+
doctest.testmod()

0 commit comments

Comments
 (0)