Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
math.sqrt, cell, floor solution in Clear category for The Nearest Square Number by kurosawa4434
from math import sqrt, ceil, floor
def nearest_square(number):
sq = sqrt(number)
c = ceil(sq)**2
f = floor(sq)**2
if abs(number - c) < abs(number - f):
return c
return f
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert nearest_square(8) == 9
assert nearest_square(13) == 16
assert nearest_square(24) == 25
assert nearest_square(9876) == 9801
May 10, 2018