Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Sparse is better than dense. solution in Clear category for The Nearest Square Number by veky
import bisect
squares = [x**2 for x in range(1001)]
def nearest_square(number):
left = bisect.bisect_left(squares, number)
right = bisect.bisect_right(squares, number) - 1
return min(squares[left], squares[right], key=lambda sq: abs(number - sq))
March 22, 2018
Comments: