Generator Expressions
Completed
Read n and print the sum of squares of the odd numbers from 1 to n, using a generator expression inside sum().
Input
One integer.
Output
The sum.
Example
Input
5
Output
35
Editor
Python
Hints
sum(expr for x in ...) needs no extra brackets.
Solution explanation
A generator expression uses almost no memory even for a million items, unlike a list comprehension.
Show solution
n = int(input())
print(sum(x * x for x in range(1, n + 1, 2)))