Sum of range in a series of first odd then even natural numbers
Last Updated :
08 Nov, 2022
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 a 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 : 27
Explanation:
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 : 4
Explanation:
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 us suppose x is less than the number of odd numbers.
- Then we simply return (x*x)
If the x is greater than the number of odd numbers
- var = x-odd;
- That means we need first var even numbers
- we return (odd*odd) + (var*var) + var;
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll sumTillX(ll x, ll n)
{
ll odd = ceil (n / 2.0);
if (x <= odd)
return x * x;
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);
}
int main()
{
ll N = 10, L = 1, R = 6;
cout << rangeSum(N, L, R);
return 0;
}
|
C
#include <stdio.h>
#include <math.h>
#define ll long long
ll sumTillX(ll x, ll n)
{
ll odd = ceil (n / 2.0);
if (x <= odd)
return x * x;
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);
}
int main()
{
ll N = 10, L = 1, R = 6;
printf ( "%d" ,rangeSum(N, L, R));
return 0;
}
|
Java
class GFG {
static double sumTillX( double x,
double n)
{
double odd = Math.ceil(n / 2.0 );
if (x <= odd)
return x * x;
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);
}
public static void main(String args[])
{
long N = 10 , L = 1 , R = 6 ;
int n = 101 ;
System.out.println(( int )rangeSum(N, L, R));
}
}
|
Python 3
import math
def sumTillX(x, n):
odd = math.ceil(n / 2.0 )
if (x < = odd):
return x * x;
even = x - odd;
return ((odd * odd) +
(even * even) + even);
def rangeSum(N, L, R):
return (sumTillX(R, N) -
sumTillX(L - 1 , N));
N = 10
L = 1
R = 6
print (rangeSum(N, L, R))
|
C#
using System;
public class GFG {
static double sumTillX( double x, double n)
{
double odd = Math.Ceiling(n / 2.0);
if (x <= odd)
return x * x;
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);
}
public static void Main()
{
long N = 10, L = 1, R = 6;
Console.Write(rangeSum(N, L, R));
}
}
|
PHP
<?php
function sumTillX( $x , $n )
{
$odd = ceil ( $n / 2.0);
if ( $x <= $odd )
return $x * $x ;
$even = $x - $odd ;
return (( $odd * $odd ) +
( $even * $even ) +
$even );
}
function rangeSum( $N , $L , $R )
{
return sumTillX( $R , $N ) -
sumTillX( $L -1, $N );
}
$N = 10; $L = 1; $R = 6;
echo (rangeSum( $N , $L , $R ));
?>
|
Javascript
<script>
function sumTillX( x, n)
{
let odd = Math.ceil(n / 2.0);
if (x <= odd)
return x * x;
let even = x - odd;
return ((odd * odd) + (even * even) + even);
}
function rangeSum( N, L, R)
{
return sumTillX(R, N) - sumTillX(L-1, N);
}
let N = 10, L = 1, R = 6;
document.write(rangeSum(N, L, R));
</script>
|
Time Complexity : O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...