Write a python function that accepts a number, and returns the nearest square number


Topic: Write a python function that accepts a number, and returns the nearest square number

Solution

import math
def nearest_square(n):
    upp = math.floor(math.sqrt(n))
    low = math.floor(math.sqrt(n))
    upp_diff = upp ** 2 - n
    low_diff = n - low ** 2
    if upp_diff > low_diff:
        return upp
    else:
        return low



List all Python Programs