The sequence first consists of all the odd numbers starting from 1 to n and then remaining even numbers starting 2 up to n. Let’s suppose we have n as 1000. Then the sequence becomes 1 3 5 7….999 2 4 6….1000

We are given a range (L, R), we need to find sum of numbers of this sequence in given range.

**Note: Here the range is given as (L, R) L and R are included in the range**

Examples:

Input : n = 10 Range 1 6 Output : 27Explanation:Sequence is 1 3 5 7 9 2 4 6 8 10 Sum in range (2, 6) = 1 + 3 + 5 + 7 + 9 + 2 = 27 Input : n = 5 Range 1 2 Output : 4Explanation:sequence is 1 3 5 2 4 sum = 1 + 3 = 4

The idea is to first find sum of numbers before left(excluding left), then find sum of numbers before right (including right). We get result as second sum minus first sum.

**How to find sum till a limit?**

We first count how many odd numbers are there, then we use formulas for sum of odd natural numbers and sum of even natural numbers to find the result.

How to find count of odd numbers?

- If n is odd then the number of odd numbers are ((n/2) + 1)
- If n is even then number of odd numbers are (n/2)

By simple observation, we get the number of odd numbers is ceil(n/2). So, the number of even numbers are n – ceil(n/2).

- Sum of first N odd numbers is (N^2)
- Sum of first N even numbers is (N^2) + N

For a given number x how will we find the sum in the sequence from 1 to x?

let’s suppose x is less than the number of odd numbers.

- Then we simply return (x*x)

If the x is greater then the number of odd numbers

## C++

`// CPP program to find sum in the given range in ` `// the sequence 1 3 5 7.....N 2 4 6...N-1 ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// For our convenience ` `#define ll long long ` ` ` `// Function that returns sum ` `// in the range 1 to x in the ` `// sequence 1 3 5 7.....N 2 4 6...N-1 ` `ll sumTillX(ll x, ll n) ` `{ ` ` ` `// number of odd numbers ` ` ` `ll odd = ` `ceil` `(n / 2.0); ` ` ` ` ` `if` `(x <= odd) ` ` ` `return` `x * x; ` ` ` ` ` `// number of extra even ` ` ` `// numbers required ` ` ` `ll even = x - odd; ` ` ` ` ` `return` `((odd * odd) + (even * even) + even); ` `} ` ` ` `int` `rangeSum(` `int` `N, ` `int` `L, ` `int` `R) ` `{ ` ` ` `return` `sumTillX(R, N) - sumTillX(L-1, N); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `ll N = 10, L = 1, R = 6; ` ` ` `cout << rangeSum(N, L, R); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find ` `// sum in the given ` `// range in the sequence ` `// 1 3 5 7.....N ` `// 2 4 6...N-1 ` ` ` `class` `GFG { ` ` ` ` ` `// Function that returns sum ` ` ` `// in the range 1 to x in the ` ` ` `// sequence 1 3 5 7.....N 2 4 6...N-1 ` ` ` `static` `double` `sumTillX(` `double` `x, ` ` ` `double` `n) ` ` ` `{ ` ` ` ` ` `// number of odd numbers ` ` ` `double` `odd = Math.ceil(n / ` `2.0` `); ` ` ` ` ` `if` `(x <= odd) ` ` ` `return` `x * x; ` ` ` ` ` `// number of extra even ` ` ` `// numbers required ` ` ` `double` `even = x - odd; ` ` ` ` ` `return` `((odd * odd) + (even * ` ` ` `even) + even); ` ` ` `} ` ` ` ` ` `static` `double` `rangeSum(` `double` `N, ` ` ` `double` `L, ` ` ` `double` `R) ` ` ` `{ ` ` ` `return` `sumTillX(R, N) - ` ` ` `sumTillX(L-` `1` `, N); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `long` `N = ` `10` `, L = ` `1` `, R = ` `6` `; ` ` ` `int` `n = ` `101` `; ` ` ` `System.out.println((` `int` `)rangeSum(N, L, R)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find sum in the ` `# given range in the sequence 1 3 5 7 ` `# .....N 2 4 6...N-1 ` `import` `math ` ` ` `# For our convenience ` `#define ll long long ` ` ` `# Function that returns sum in the ` `# range 1 to x in the sequence ` `# 1 3 5 7.....N 2 4 6...N-1 ` `def` `sumTillX(x, n): ` ` ` ` ` `# number of odd numbers ` ` ` `odd ` `=` `math.ceil(n ` `/` `2.0` `) ` ` ` ` ` `if` `(x <` `=` `odd): ` ` ` `return` `x ` `*` `x; ` ` ` ` ` `# number of extra even ` ` ` `# numbers required ` ` ` `even ` `=` `x ` `-` `odd; ` ` ` ` ` `return` `((odd ` `*` `odd) ` `+` ` ` `(even ` `*` `even) ` `+` `even); ` ` ` ` ` `def` `rangeSum(N, L, R): ` ` ` ` ` `return` `(sumTillX(R, N) ` `-` ` ` `sumTillX(L` `-` `1` `, N)); ` ` ` `# Driver code ` `N ` `=` `10` `L ` `=` `1` `R ` `=` `6` `print` `(rangeSum(N, L, R)) ` ` ` `# This code is contributed by ` `# Smitha ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum in the given ` `// range in the sequence 1 3 5 7.....N ` `// 2 4 6...N-1 ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// Function that returns sum ` ` ` `// in the range 1 to x in the ` ` ` `// sequence 1 3 5 7.....N 2 4 6...N-1 ` ` ` `static` `double` `sumTillX(` `double` `x, ` `double` `n) ` ` ` `{ ` ` ` ` ` `// number of odd numbers ` ` ` `double` `odd = Math.Ceiling(n / 2.0); ` ` ` ` ` `if` `(x <= odd) ` ` ` `return` `x * x; ` ` ` ` ` `// number of extra even ` ` ` `// numbers required ` ` ` `double` `even = x - odd; ` ` ` ` ` `return` `((odd * odd) + (even * even) ` ` ` `+ even); ` ` ` `} ` ` ` ` ` `static` `double` `rangeSum(` `double` `N, ` `double` `L, ` ` ` `double` `R) ` ` ` `{ ` ` ` `return` `sumTillX(R, N) - sumTillX(L-1, N); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `long` `N = 10, L = 1, R = 6; ` ` ` `Console.Write(rangeSum(N, L, R)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find sum ` `// in the given range in the ` `// sequence 1 3 5 7..... ` `// N 2 4 6...N-1 ` ` ` `// Function that returns sum ` `// in the range 1 to x in the ` `// sequence 1 3 5 7..... ` `// N 2 4 6...N-1 ` `function` `sumTillX(` `$x` `, ` `$n` `) ` `{ ` ` ` ` ` `// number of odd numbers ` ` ` `$odd` `= ` `ceil` `(` `$n` `/ 2.0); ` ` ` ` ` `if` `(` `$x` `<= ` `$odd` `) ` ` ` `return` `$x` `* ` `$x` `; ` ` ` ` ` `// number of extra even ` ` ` `// numbers required ` ` ` `$even` `= ` `$x` `- ` `$odd` `; ` ` ` ` ` `return` `((` `$odd` `* ` `$odd` `) + ` ` ` `(` `$even` `* ` `$even` `) + ` ` ` `$even` `); ` `} ` ` ` `function` `rangeSum(` `$N` `, ` `$L` `, ` `$R` `) ` `{ ` ` ` `return` `sumTillX(` `$R` `, ` `$N` `) - ` ` ` `sumTillX(` `$L` `-1, ` `$N` `); ` `} ` ` ` `// Driver code ` `$N` `= 10; ` `$L` `= 1; ` `$R` `= 6; ` `echo` `(rangeSum(` `$N` `, ` `$L` `, ` `$R` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

27

## Recommended Posts:

- Count of integers in a range which have even number of odd digits and odd number of even digits
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Sum of sum-series of first N Natural numbers
- Sum of cubes of even and odd natural numbers
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Sum of all odd natural numbers in range L and R
- Sum of cubes of first n odd natural numbers
- Sum of fourth powers of first n odd natural numbers
- Sum of fourth power of first n even natural numbers
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Average of first n even natural numbers
- Sum of series formed by difference between product and sum of N natural numbers
- Sum of array elements that is first continuously increasing then decreasing
- Difference between sum of the squares of first n natural numbers and square of sum
- Sum of sum of all subsets of a set formed by first N natural numbers
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given 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 contribute@geeksforgeeks.org. 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.