From 17516b625e01065e5a8a29af7088e178313849c7 Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Mon, 30 Mar 2020 18:27:37 -0700 Subject: [PATCH] Use math.Sqrt for IntegerSquareRoot (#5253) * use std * Merge refs/heads/master into faster-sqrt --- shared/mathutil/math_helper.go | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/shared/mathutil/math_helper.go b/shared/mathutil/math_helper.go index de31bf20fb78..c3c603197528 100644 --- a/shared/mathutil/math_helper.go +++ b/shared/mathutil/math_helper.go @@ -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