Sum of the series 1, 2, 4, 3, 5, 7, 9, 6, 8, 10, 11, 13.. till N-th term
Given a series of numbers 1, 2, 4, 3, 5, 7, 9, 6, 8, 10, 11, 13… The task is to find the sum of all the numbers in series till N-th number.
Examples:
Input: N = 4
Output: 10
1 + 2 + 4 + 3 = 10
Input: N = 10
Output: 55
Approach: The series is basically 20 odd numbers, 21 even numbers, 22 even numbers…. The sum of first N odd numbers is N * N and sum of first N even numbers is (N * (N+1)). Calculate the summation for 2i odd or even numbers and keep adding them to the sum.
Iterate for every power of 2, till the number of iterations exceeds N, and keep adding the respective summation of odd or even numbers according to the parity. For every segment the sum of the segment will be, (current sum of X odd/even numbers – previous sum of Y odd/even numbers), where X is the total sum of odd/even numbers till this segment and Y is the summation of odd/even numbers till the previous when odd/even numbers occurred.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sumodd( int n)
{
return (n * n);
}
int sumeven( int n)
{
return (n * (n + 1));
}
int findSum( int num)
{
int sumo = 0;
int sume = 0;
int x = 1;
int cur = 0;
int ans = 0;
while (num > 0) {
int inc = min(x, num);
num -= inc;
if (cur == 0) {
ans = ans + sumodd(sumo + inc) - sumodd(sumo);
sumo += inc;
}
else {
ans = ans + sumeven(sume + inc) - sumeven(sume);
sume += inc;
}
x *= 2;
cur ^= 1;
}
return ans;
}
int main()
{
int n = 4;
cout << findSum(n);
return 0;
}
|
Java
class GFG
{
static int sumodd( int n)
{
return (n * n);
}
static int sumeven( int n)
{
return (n * (n + 1 ));
}
static int findSum( int num)
{
int sumo = 0 ;
int sume = 0 ;
int x = 1 ;
int cur = 0 ;
int ans = 0 ;
while (num > 0 )
{
int inc = Math.min(x, num);
num -= inc;
if (cur == 0 )
{
ans = ans + sumodd(sumo + inc) - sumodd(sumo);
sumo += inc;
}
else
{
ans = ans + sumeven(sume + inc) - sumeven(sume);
sume += inc;
}
x *= 2 ;
cur ^= 1 ;
}
return ans;
}
public static void main(String[] args)
{
int n = 4 ;
System.out.println(findSum(n));
}
}
|
Python
def sumodd(n):
return (n * n)
def sumeven(n):
return (n * (n + 1 ))
def findSum(num):
sumo = 0
sume = 0
x = 1
cur = 0
ans = 0
while (num > 0 ):
inc = min (x, num)
num - = inc
if (cur = = 0 ):
ans = ans + sumodd(sumo + inc) - sumodd(sumo)
sumo + = inc
else :
ans = ans + sumeven(sume + inc) - sumeven(sume)
sume + = inc
x * = 2
cur ^ = 1
return ans
n = 4
print (findSum(n))
|
C#
using System;
class GFG
{
static int sumodd( int n)
{
return (n * n);
}
static int sumeven( int n)
{
return (n * (n + 1));
}
static int findSum( int num)
{
int sumo = 0;
int sume = 0;
int x = 1;
int cur = 0;
int ans = 0;
while (num > 0)
{
int inc = Math.Min(x, num);
num -= inc;
if (cur == 0)
{
ans = ans + sumodd(sumo + inc) - sumodd(sumo);
sumo += inc;
}
else
{
ans = ans + sumeven(sume + inc) - sumeven(sume);
sume += inc;
}
x *= 2;
cur ^= 1;
}
return ans;
}
public static void Main(String[] args)
{
int n = 4;
Console.WriteLine(findSum(n));
}
}
|
PHP
<?php
function sumodd( $n )
{
return ( $n * $n );
}
function sumeven( $n )
{
return ( $n * ( $n + 1));
}
function findSum( $num )
{
$sumo = 0;
$sume = 0;
$x = 1;
$cur = 0;
$ans = 0;
while ( $num > 0)
{
$inc = min( $x , $num );
$num -= $inc ;
if ( $cur == 0)
{
$ans = $ans + sumodd( $sumo + $inc ) -
sumodd( $sumo );
$sumo += $inc ;
}
else
{
$ans = $ans + sumeven( $sume + $inc ) -
sumeven( $sume );
$sume += $inc ;
}
$x *= 2;
$cur ^= 1;
}
return $ans ;
}
$n = 4;
echo findSum( $n );
?>
|
Javascript
<script>
function sumodd( n)
{
return (n * n);
}
function sumeven( n)
{
return (n * (n + 1));
}
function findSum( num)
{
let sumo = 0;
let sume = 0;
let x = 1;
let cur = 0;
let ans = 0;
while (num > 0) {
let inc = Math.min(x, num);
num -= inc;
if (cur == 0) {
ans = ans + sumodd(sumo + inc) - sumodd(sumo);
sumo += inc;
}
else {
ans = ans + sumeven(sume + inc) - sumeven(sume);
sume += inc;
}
x *= 2;
cur ^= 1;
}
return ans;
}
let n = 4;
document.write( findSum(n));
</script>
|
Time Complexity: O(n) because using inbuilt function min
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
05 Aug, 2022
Like Article
Save Article