File tree Expand file tree Collapse file tree 1 file changed +7
-8
lines changed Expand file tree Collapse file tree 1 file changed +7
-8
lines changed Original file line number Diff line number Diff line change
1
+ import decimal
1
2
import functools
2
3
import itertools
3
4
from typing import (
@@ -103,8 +104,7 @@ def clamp(inclusive_lower_bound: int,
103
104
104
105
def integer_squareroot (value : int ) -> int :
105
106
"""
106
- Return the largest integer ``x`` such that ``x**2 <= value``.
107
- Ref: https://en.wikipedia.org/wiki/Integer_square_root
107
+ Return the integer square root of ``value``.
108
108
"""
109
109
if not isinstance (value , int ) or isinstance (value , bool ):
110
110
raise ValueError (
@@ -119,9 +119,8 @@ def integer_squareroot(value: int) -> int:
119
119
)
120
120
)
121
121
122
- x = value
123
- y = (x + 1 ) // 2
124
- while y < x :
125
- x = y
126
- y = (x + value // x ) // 2
127
- return x
122
+ with decimal .localcontext () as ctx :
123
+ # Set precision to 128, since the largest square root of a
124
+ # 256-bit integer is a 128-bit integer.
125
+ ctx .prec = 128
126
+ return int (decimal .Decimal (value ).sqrt ())
You can’t perform that action at this time.
0 commit comments