Created
December 2, 2020 06:19
-
-
Save angeloped/0056c737b5870f99a8f2334d27e6854e to your computer and use it in GitHub Desktop.
Determine the nearest perfect square within the range from 0 to n.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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