Editor

Total of the Odd Numbers

#525 · Loops · Beginner · 25 XP · about 3 min

Write sum_of_odds(start, end) that adds up all the odd numbers from start to end, including start and end themselves when they are odd.

If start is bigger than end, there are no numbers to add, so the total is 0.

Examples

sum_of_odds(1, 10)  # returns 25
sum_of_odds(4, 9)   # returns 21
sum_of_odds(7, 7)   # returns 7
sum_of_odds(10, 2)  # returns 0

From 1 to 10 the odd numbers are 1, 3, 5, 7 and 9, which add up to 25.

Note

The numbers can be negative: -3 and -1 are odd too.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.