Skip to content

Instantly share code, notes, and snippets.

@danielchikaka
Last active April 20, 2022 06:23
Show Gist options
  • Save danielchikaka/fcaae1638484ee6294935ce4d76f0a38 to your computer and use it in GitHub Desktop.
Save danielchikaka/fcaae1638484ee6294935ce4d76f0a38 to your computer and use it in GitHub Desktop.
Complete the findNextSquare method that finds the next integral perfect square after the one passed as a parameter. Recall that an integral perfect square is an integer n such that sqrt(n) is also an integer. If the parameter is itself not a perfect square then -1 should be returned. You may assume the parameter is non-negative.
# import math
# def find_next_square(sq):
# # Return the next square if sq is a square, -1 otherwise
# if sq % math.sqrt(sq) == 0:
# sq_number = math.sqrt(sq)
# new_sq_sq = (sq_number + 1)**2
# if new_sq_sq % math.sqrt(new_sq_sq) == 0:
# return new_sq_sq
# else:
# return -1
# else:
# return -1
def find_next_square(sq):
root = sq ** 0.5
if root.is_integer():
return (root + 1) ** 2
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment