Sum of the series 1, 2, 4, 3, 5, 7, 9, 6, 8, 10, 11, 13.. till N-th term - GeeksforGeeks

10 Oct, 2021
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++
Java
Python
C#
PHP
Javascript
// C++ program to implement// the above approach #include <bits/stdc++.h>using namespace std; // Function to find the// sum of first N odd numbersint sumodd(int n){    return (n * n);} // Function to find the// sum of first N even numbersint sumeven(int n){    return (n * (n + 1));} // Function to overall// find the sum of seriesint findSum(int num){     // Initial odd numbers    int sumo = 0;     // Initial even numbers    int sume = 0;     // First power of 2    int x = 1;     // Check for parity    // for odd/even    int cur = 0;     // Counts the sum    int ans = 0;    while (num > 0) {         // Get the minimum        // out of remaining num        // or power of 2        int inc = min(x, num);         // Decrease that much numbers        // from num        num -= inc;         // If the segment has odd numbers        if (cur == 0) {             // Summate the odd numbers            // By exclusion            ans = ans + sumodd(sumo + inc) - sumodd(sumo);             // Increase number of odd numbers            sumo += inc;        }        // If the segment has even numbers        else {             // Summate the even numbers            // By exclusion            ans = ans + sumeven(sume + inc) - sumeven(sume);             // Increase number of even numbers            sume += inc;        }         // Next set of numbers        x *= 2;         // Change parity for odd/even        cur ^= 1;    }     return ans;} // Driver codeint main(){    int n = 4;    cout << findSum(n);     return 0;}
// Java program to implement// the above approach class GFG{     // Function to find the    // sum of first N odd numbers    static int sumodd(int n)    {        return (n * n);    }     // Function to find the    // sum of first N even numbers    static int sumeven(int n)    {        return (n * (n + 1));    }     // Function to overall    // find the sum of series    static int findSum(int num)    {         // Initial odd numbers        int sumo = 0;         // Initial even numbers        int sume = 0;         // First power of 2        int x = 1;         // Check for parity        // for odd/even        int cur = 0;         // Counts the sum        int ans = 0;        while (num > 0)        {             // Get the minimum            // out of remaining num            // or power of 2            int inc = Math.min(x, num);             // Decrease that much numbers            // from num            num -= inc;             // If the segment has odd numbers            if (cur == 0)            {                 // Summate the odd numbers                // By exclusion                ans = ans + sumodd(sumo + inc) - sumodd(sumo);                 // Increase number of odd numbers                sumo += inc;            }                         // If the segment has even numbers            else            {                 // Summate the even numbers                // By exclusion                ans = ans + sumeven(sume + inc) - sumeven(sume);                 // Increase number of even numbers                sume += inc;            }             // Next set of numbers            x *= 2;             // Change parity for odd/even            cur ^= 1;        }         return ans;    }     // Driver code    public static void main(String[] args)    {        int n = 4;        System.out.println(findSum(n));     }} // This code contributed by Rajput-Ji
# Python3 program to implement# the above approach # Function to find the# sum of first N odd numbersdef sumodd(n):     return (n * n) # Function to find the# sum of first N even numbersdef sumeven(n):     return (n * (n + 1))  # Function to overall# find the sum of seriesdef findSum(num):      # Initial odd numbers    sumo = 0     # Initial even numbers    sume = 0     # First power of 2    x = 1     # Check for parity    # for odd/even    cur = 0     # Counts the sum    ans = 0    while (num > 0):         # Get the minimum        # out of remaining num        # or power of 2        inc = min(x, num)         # Decrease that much numbers        # from num        num -= inc         # If the segment has odd numbers        if (cur == 0):             # Summate the odd numbers            # By exclusion            ans = ans + sumodd(sumo + inc) - sumodd(sumo)             # Increase number of odd numbers            sumo += inc                 # If the segment has even numbers        else:             # Summate the even numbers            # By exclusion            ans = ans + sumeven(sume + inc) - sumeven(sume)             # Increase number of even numbers            sume += inc                  # Next set of numbers        x *= 2         # Change parity for odd/even        cur ^= 1         return ans # Driver coden = 4print(findSum(n)) # This code is contributed by mohit kumar
// C# program to implement// the above approachusing System; class GFG{     // Function to find the    // sum of first N odd numbers    static int sumodd(int n)    {        return (n * n);    }     // Function to find the    // sum of first N even numbers    static int sumeven(int n)    {        return (n * (n + 1));    }     // Function to overall    // find the sum of series    static int findSum(int num)    {         // Initial odd numbers        int sumo = 0;         // Initial even numbers        int sume = 0;         // First power of 2        int x = 1;         // Check for parity        // for odd/even        int cur = 0;         // Counts the sum        int ans = 0;        while (num > 0)        {             // Get the minimum            // out of remaining num            // or power of 2            int inc = Math.Min(x, num);             // Decrease that much numbers            // from num            num -= inc;             // If the segment has odd numbers            if (cur == 0)            {                 // Summate the odd numbers                // By exclusion                ans = ans + sumodd(sumo + inc) - sumodd(sumo);                 // Increase number of odd numbers                sumo += inc;            }                         // If the segment has even numbers            else            {                 // Summate the even numbers                // By exclusion                ans = ans + sumeven(sume + inc) - sumeven(sume);                 // Increase number of even numbers                sume += inc;            }             // Next set of numbers            x *= 2;             // Change parity for odd/even            cur ^= 1;        }         return ans;    }     // Driver code    public static void Main(String[] args)    {        int n = 4;        Console.WriteLine(findSum(n));     }} // This code has been contributed by 29AjayKumar
<?php// PHP program to implement// the above approach // Function to find the// sum of first N odd numbersfunction sumodd($n){    return ($n * $n);} // Function to find the// sum of first N even numbersfunction sumeven($n){    return ($n * ($n + 1));} // Function to overall// find the sum of seriesfunction findSum( $num){     // Initial odd numbers    $sumo = 0;     // Initial even numbers    $sume = 0;     // First power of 2    $x = 1;     // Check for parity    // for odd/even    $cur = 0;     // Counts the sum    $ans = 0;    while ($num > 0)    {         // Get the minimum        // out of remaining num        // or power of 2        $inc = min($x, $num);         // Decrease that much numbers        // from num        $num -= $inc;         // If the segment has odd numbers        if ($cur == 0)        {             // Summate the odd numbers            // By exclusion            $ans = $ans + sumodd($sumo + $inc) -                          sumodd($sumo);             // Increase number of odd numbers            $sumo += $inc;        }                 // If the segment has even numbers        else        {             // Summate the even numbers            // By exclusion            $ans = $ans + sumeven($sume + $inc) -                          sumeven($sume);             // Increase number of even numbers            $sume += $inc;        }         // Next set of numbers        $x *= 2;         // Change parity for odd/even        $cur ^= 1;    }     return $ans;} // Driver code$n = 4;echo findSum($n); // This code contributed by princiraj1992?>
<script> // javascript program to implement// the above approach // Function to find the// sum of first N odd numbersfunction sumodd( n){    return (n * n);} // Function to find the// sum of first N even numbersfunction sumeven( n){    return (n * (n + 1));} // Function to overall// find the sum of seriesfunction findSum( num){     // Initial odd numbers    let sumo = 0;     // Initial even numbers    let sume = 0;     // First power of 2    let x = 1;     // Check for parity    // for odd/even    let cur = 0;     // Counts the sum    let ans = 0;    while (num > 0) {         // Get the minimum        // out of remaining num        // or power of 2        let inc = Math.min(x, num);         // Decrease that much numbers        // from num        num -= inc;         // If the segment has odd numbers        if (cur == 0) {             // Summate the odd numbers            // By exclusion            ans = ans + sumodd(sumo + inc) - sumodd(sumo);             // Increase number of odd numbers            sumo += inc;        }        // If the segment has even numbers        else {             // Summate the even numbers            // By exclusion            ans = ans + sumeven(sume + inc) - sumeven(sume);             // Increase number of even numbers            sume += inc;        }         // Next set of numbers        x *= 2;         // Change parity for odd/even        cur ^= 1;    }     return ans;} // Driver code     let n = 4;    document.write( findSum(n));     // This code is contributed by todaysgaurav  </script>
10
 
mohit kumar 29
Rajput-Ji
29AjayKumar
princiraj1992
todaysgaurav
hritikbhatnagar2182
simmytarika5
series
series-sum
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Modular multiplicative inverse
Fizz Buzz Implementation
Generate all permutation of a set in Python
Check if a number is Palindrome
How to check if a given point lies inside or outside a polygon?
Program to multiply two matrices
Segment Tree | Set 1 (Sum of given range)
Merge two sorted arrays with O(1) extra space
Singular Value Decomposition (SVD)