Skip to content

Commit

Permalink
Use math.Sqrt for IntegerSquareRoot (#5253)
Browse files Browse the repository at this point in the history
* use std
* Merge refs/heads/master into faster-sqrt
  • Loading branch information
prestonvanloon authored Mar 31, 2020
1 parent 7f7866f commit 17516b6
Showing 1 changed file with 2 additions and 21 deletions.
23 changes: 2 additions & 21 deletions shared/mathutil/math_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,13 @@ var squareRootTable = map[uint64]uint64{
}

// IntegerSquareRoot defines a function that returns the
// largest possible integer root of a number using a divide and conquer
// binary search approach:
//
// inspiration: https://www.geeksforgeeks.org/square-root-of-an-integer
// largest possible integer root of a number using go's standard library.
func IntegerSquareRoot(n uint64) uint64 {
if v, ok := squareRootTable[n]; ok {
return v
}

x := uint64(0)
y := uint64(1 << 32)
for {
if y <= 1+x {
return x
}
sqt := x + ((y - x) >> 1)
sq := sqt * sqt
if sq == n {
return sqt
}
if sq > n {
y = sqt
} else {
x = sqt
}
}
return uint64(math.Sqrt(float64(n)))
}

// CeilDiv8 divides the input number by 8
Expand Down

0 comments on commit 17516b6

Please sign in to comment.