Given two integers L and R, the task is to find the sum of all odd natural numbers in range L and R inclusive.
Input: L = 2, R = 5 Output: 8 3 + 5 = 8 Input: L = 7, R = 13 Output: 40
A naive approach is to traverse from L to R and summate the elements to get the answer.
An efficient approach is to use the formula for calculating the sum of all odd natural numbers upto R and L-1 and then subtract sum(R)-sum(L-1).
Below is the implementation of the above approach:
Sum of odd natural numbers from L to R is 8
- Sum of all natural numbers in range L to R
- Sum of range in a series of first odd then even natural numbers
- Greatest divisor which divides all natural number in range [L, R]
- Natural Numbers
- Sum of sum of first n natural numbers
- LCM of First n Natural Numbers
- Sum of first N natural numbers which are divisible by 2 and 7
- Sum of first N natural numbers which are not powers of K
- Sum of cubes of even and odd natural numbers
- Sum of kth powers of first n natural numbers
- Sum of cubes of first n odd natural numbers
- Average of first n even natural numbers
- Sum of squares of first n natural numbers
- Repeated sum of first N natural numbers
- Sum of fifth powers of the first n natural numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.