-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathisqrt.go
More file actions
46 lines (41 loc) · 817 Bytes
/
isqrt.go
File metadata and controls
46 lines (41 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// python 里的math.isqrt()函数使用牛顿迭代法,避免了浮点数转整数的误差
// https://leetcode.cn/problems/sqrtx/solution/x-de-ping-fang-gen-by-leetcode-solution/
package main
import (
"fmt"
"math"
)
func Isqrt(x int) int {
x0 := x >> 1
if x0 == 0 {
return x
}
x1 := (x0 + x/x0) >> 1
for x1 < x0 {
x0 = x1
x1 = (x0 + x/x0) >> 1
}
return x0
}
// 返回floor(sqrt(x)).x为负数时返回0.
func Isqrt2(x int) int {
if x <= 0 {
return 0
}
sqrt := int(math.Sqrt(float64(x)))
for (sqrt+1)*(sqrt+1) <= x {
sqrt++
}
for sqrt*sqrt > x {
sqrt--
}
return sqrt
}
func main() {
fmt.Println(Isqrt(5))
// check 1e18 to 1e18+100
for i := 0; i < 100; i++ {
fmt.Println(Isqrt(1000000000000000000 + i))
}
fmt.Println(Isqrt2(5))
}