Last active
April 20, 2022 06:23
-
-
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.
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
# 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