Created
May 12, 2023 20:53
-
-
Save les-peters/46473d224689a45a765706ec9e53cece to your computer and use it in GitHub Desktop.
Sum Odd Squares
This file contains 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
question = """ | |
Sum the odd-square numbers less than a given integer n. | |
Example: | |
> oddSquareSum(1) | |
> 0 | |
> oddSquareSum(2) | |
> 1 | |
> oddSquareSum(9) | |
> 1 | |
> oddSquareSum(10) | |
> 10 | |
> oddSquareSum(44) | |
> 35 | |
""" | |
def oddSquareSum(n): | |
# print('n =' + str(n)) | |
sum = 0 | |
i = 0 | |
x = ((2 * i) + 1)**2 | |
# print('x ' + str(x)) | |
while x < n: | |
if x < n: | |
sum = sum + x | |
i = i + 1 | |
x = ((2 * i) + 1)**2 | |
# print('x ' + str(x)) | |
return sum | |
print('sum ' + str(oddSquareSum(1))) | |
print('sum ' + str(oddSquareSum(2))) | |
print('sum ' + str(oddSquareSum(9))) | |
print('sum ' + str(oddSquareSum(10))) | |
print('sum ' + str(oddSquareSum(44))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment