Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)!
Last Updated :
07 Jan, 2024
Given a number x and n, the task is to find the sum of the below series of x till n terms:
Examples:
Input: x = 5, n = 2
Output: 7.67
Explanation:
Sum of first two termed
Input: x = 5, n = 4Output: 18.08Explanation:
Approach: Iterate the loop till the nth term, compute the formula in each iteration i.e.
nth term of the series =
Below is the implementation of the above approach:
C++
#include <iostream>
#include <math.h>
using namespace std;
int fact( int n)
{
if (n == 1)
return 1;
return n * fact(n - 1);
}
double sum( int x, int n)
{
double i, total = 1.0;
for (i = 1; i <= n; i++) {
total = total + ( pow (x, i) / fact(i + 1));
}
return total;
}
int main()
{
int x = 5, n = 4;
cout << "Sum is: " << sum(x, n);
return 0;
}
|
Java
public class SumOfSeries {
static int fact( int n)
{
if (n == 1 )
return 1 ;
return n * fact(n - 1 );
}
static double sum( int x, int n)
{
double total = 1.0 ;
for ( int i = 1 ; i <= n; i++) {
total = total + (Math.pow(x, i) / fact(i + 1 ));
}
return total;
}
public static void main(String[] args)
{
int x = 5 , n = 4 ;
System.out.print( "Sum is: " + sum(x, n));
}
}
|
Python3
def fact(n):
if n = = 1 :
return 1
else :
return n * fact(n - 1 )
def sum (x, n):
total = 1.0
for i in range ( 1 , n + 1 , 1 ):
total = total + ( pow (x, i) / fact(i + 1 ))
return total
if __name__ = = '__main__' :
x = 5
n = 4
print ( "Sum is: {0:.4f}" . format ( sum (x, n)))
|
C#
using System;
class SumOfSeries {
static int fact( int n)
{
if (n == 1)
return 1;
return n * fact(n - 1);
}
static double sum( int x, int n)
{
double total = 1.0;
for ( int i = 1; i <= n; i++) {
total = total + (Math.Pow(x, i) / fact(i + 1));
}
return total;
}
public static void Main()
{
int x = 5, n = 4;
Console.WriteLine( "Sum is: " + sum(x, n));
}
}
|
Javascript
<script>
function fact(n)
{
if (n == 1)
return 1;
return n * fact(n - 1);
}
function sum(x, n)
{
let total = 1.0;
for (let i = 1; i <= n; i++)
{
total = total + (Math.pow(x, i) /
fact(i + 1));
}
return total.toFixed(4);
}
let x = 5;
let n = 4;
document.write( "Sum is: " + sum(x, n));
</script>
|
PHP
<?php
function fact( $n )
{
if ( $n == 1)
return 1;
return $n * fact( $n - 1);
}
function sum( $x , $n )
{
$total = 1.0;
for ( $i = 1; $i <= $n ; $i ++)
{
$total = $total + (pow( $x , $i ) /
fact( $i + 1));
}
return $total ;
}
$x = 5;
$n = 4;
echo "Sum is: " , sum( $x , $n );
?>
|
Time Complexity: O(n2)
Auxiliary Space: O(n), since n extra space has been taken.
Efficient approach: Time complexity for above algorithm is O() because for each sum iteration factorial is being calculated which is O(n). It can be observed that term of the series can be written as , where . Now we can iterate over to calculate the sum.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
double sum( int x, int n)
{
double total = 1.0;
double previous = 1.0;
for ( int i = 1; i <= n; i++)
{
previous = (previous * x) / (i + 1);
total = total + previous;
}
return total;
}
int main()
{
int x = 5, n = 4;
cout << "Sum is: " << sum(x, n);
return 0;
}
|
Java
public class GFG {
static double sum( int x, int n)
{
double total = 1.0 ;
double previous = 1.0 ;
for ( int i = 1 ; i <= n; i++) {
previous = (previous * x) / (i + 1 );
total = total + previous;
}
return total;
}
public static void main(String[] args)
{
int x = 5 , n = 4 ;
System.out.print( "Sum is: " + sum(x, n));
}
}
|
Python3
def sum (x, n):
total = 1.0 ;
previous = 1.0 ;
for i in range ( 1 , n + 1 ):
previous = (previous * x) / (i + 1 );
total = total + previous;
return total;
if __name__ = = '__main__' :
x = 5 ;
n = 4 ;
print ( "Sum is: " , sum (x, n));
|
C#
using System;
class GFG
{
public double sum( int x, int n)
{
double total = 1.0;
double previous = 1.0;
for ( int i = 1; i <= n; i++)
{
previous = ((previous * x) / (i + 1));
total = total + previous;
}
return total;
}
}
class geek
{
public static void Main()
{
GFG g = new GFG();
int x = 5, n = 4;
Console.WriteLine( "Sum is: " + g.sum(x, n));
}
}
|
Javascript
<script>
function sum(x, n)
{
let total = 1.0;
let previous = 1.0;
for (let i = 1; i <= n; i++)
{
previous = ((previous * x) / (i + 1));
total = total + previous;
}
return total;
}
let x = 5, n = 4;
document.write( "Sum is: " + sum(x, n));
</script>
|
OutputSum is: 18.083333333333336
Time Complexity: O(n)
Auxiliary Space: O(1) since using constant variables
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...