Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++



In this article, we are given a mathematical series. Our task is to write a program to find the sum of the series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n. This can also be represented as: $$ 1+\displaystyle\sum\limits_{k=1}^n \left(\frac{x^k}{k}\right) $$

This series without starting 1 is known as the Taylor Expansion Series for -ln(1-x) where ln is the natural log.

Example

Here is an example of calculating the value of the given series:

Input:
x = 7, n = 4

Output:
747.08

Here is the explanation of the above example:

For x = 7 and n = 4,
Given sequence = 1 + x + (x^2)/2 + (x^3)/3 + (x^4)/4
= 1 + 7 + (7^2)/2 + (7^3)/3 + (7^4)/4
= 1 + 7 + 24.5 + 114.33 + 600.25
= 747.08

Sum of Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n Using pow()

In this approach, we have used the pow() function of <math.h> library to calculate the power of x^i and divide this value by i. We sum this value for each i and return the total sum.

Example

The following example uses the pow() function to calculate the sum of the given series:

#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

double seriesSum(int x, int n)
{
   double i, total = 1.0;
   for (i = 1; i <= n; i++)
      total += (pow(x, i) / i);
   return total;
}
int main()
{
   int x = 3;
   int n = 6;
   cout << "The value of x: " << x << " and n is: " << n;  
   cout << endl;
   cout << "Sum of the Series: 1 + x/1 + x^2/2 + ... + x^"
        << n << "/" << n << " is: "
        << setprecision(5) << seriesSum(x, n);
   return 0;
}

The output of the above code is as follows:

The value of x: 3 and n is: 6
Sum of the Series: 1 + x/1 + x^2/2 + ... + x^6/6 is: 207.85

Sum of Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n Using Iterative Power Method

In this approach instead of calculating power in each iteration using the pow() function, we reuse the previous value for calculating the power. The term calculates and stores the power x^i and the total stores and divides the power value x^i by i. After the for loop completes, the total value is returned.

Example

In this example, we have calculated the sum of the sequence without using the pow() function by reusing the previous values:

#include <iostream>
#include <iomanip>
using namespace std;

double seriesSum(int x, int n)
{
   double term = 1.0;
   double total = 1.0;

   for (int i = 1; i <= n; i++)
   {
      // We are computing x^i using previous term
      term *= x;
      // Adding x^i / i
      total += term / i;
   }
   return total;
}

int main()
{
   int x = 3;
   int n = 6;
   cout << "The value of x: " << x << " and n is: " << n;  
   cout << endl;
   cout << "Sum of the Series 1 + x/1 + x^2/2 + ... + x^"
        << n << "/" << n << " is: "
        << setprecision(5) << seriesSum(x, n);
   return 0;
}

The output of the above code is as follows:

The value of x: 3 and n is: 6
Sum of the Series 1 + x/1 + x^2/2 + ... + x^6/6 is: 207.85

Complexity Comparison

Here is a comparison of time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
pow() Function O(n log n) O(1)
Iterative Power O(n) O(1)
Updated on: 2025-07-01T15:33:23+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements