Skip to content

Commit 203d358

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 015c487 commit 203d358

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

maths/perfect_cube.py

Lines changed: 14 additions & 14 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)
@@ -93,29 +93,29 @@ def perfect_cube_binary_search(n: int) -> bool:
9393
"""
9494
if not isinstance(n, int):
9595
raise TypeError("perfect_cube_binary_search() only accepts integers")
96-
96+
9797
# Handle zero and negative numbers
9898
if n == 0:
9999
return True
100100
if n < 0:
101101
n = -n
102-
102+
103103
# Quick checks to eliminate obvious non-cubes
104104
# Check last three digits using modulo arithmetic
105105
# Only 0, 1, 8, 7, 4, 5, 6, 3, 2, 9 can be cubes mod 10
106106
# But for cubes, the pattern is more complex
107107
last_digit = n % 10
108108
if last_digit not in {0, 1, 8, 7, 4, 5, 6, 3, 2, 9}:
109109
return False
110-
110+
111111
# More refined check: cubes mod 7 can only be 0, 1, 6
112112
if n % 7 not in {0, 1, 6}:
113113
return False
114-
114+
115115
# More refined check: cubes mod 9 can only be 0, 1, 8
116116
if n % 9 not in {0, 1, 8}:
117117
return False
118-
118+
119119
# Estimate the cube root using logarithms for very large numbers
120120
# This gives us a much better initial right bound
121121
if n > 10**18:
@@ -128,7 +128,7 @@ def perfect_cube_binary_search(n: int) -> bool:
128128
else:
129129
# For smaller numbers, use the standard approach
130130
left, right = 0, n // 2 + 1
131-
131+
132132
# Binary search
133133
while left <= right:
134134
mid = (left + right) // 2
@@ -137,19 +137,19 @@ def perfect_cube_binary_search(n: int) -> bool:
137137
if mid > 10**6 and mid * mid > n // mid:
138138
right = mid - 1
139139
continue
140-
140+
141141
cube = mid * mid * mid
142142
if cube == n:
143143
return True
144144
elif cube < n:
145145
left = mid + 1
146146
else:
147147
right = mid - 1
148-
148+
149149
return False
150150

151151

152152
if __name__ == "__main__":
153153
import doctest
154154

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

0 commit comments

Comments
 (0)