Skip to content

Instantly share code, notes, and snippets.

@les-peters
Created May 12, 2023 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save les-peters/46473d224689a45a765706ec9e53cece to your computer and use it in GitHub Desktop.
Save les-peters/46473d224689a45a765706ec9e53cece to your computer and use it in GitHub Desktop.
Sum Odd Squares
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