Skip to content

Instantly share code, notes, and snippets.

@angeloped
Created December 2, 2020 06:19
Show Gist options
  • Save angeloped/0056c737b5870f99a8f2334d27e6854e to your computer and use it in GitHub Desktop.
Save angeloped/0056c737b5870f99a8f2334d27e6854e to your computer and use it in GitHub Desktop.
Determine the nearest perfect square within the range from 0 to n.
def near_square(n):
for i in range(n,0,-1):
if (i**(1.0/2))%1==0: # if perfect square
break
return i
print(near_square(26))
print(near_square(10))
print(near_square(9))
print(near_square(4))
print(near_square(2))
print(near_square(3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment