write a story starting from. at first i paid no attention to the strangely dressed old woman standing near the statue...
Answers
Answer:
GEEKSFORGEEKS
Program to get the Sum of series: 1 – x^2/2! + x^4/4! -…. upto nth term
This is a mathematical series program where a user must enter the number of terms up to which the sum of the series is to be found. Following this, we also need the value of x, which forms the base of the series.
Examples:
Input : x = 9, n = 10
Output : -5.1463
Explanation:
GEEKSFORGEEKS
Program to get the Sum of series: 1 – x^2/2! + x^4/4! -…. upto nth term
This is a mathematical series program where a user must enter the number of terms up to which the sum of the series is to be found. Following this, we also need the value of x, which forms the base of the series.
Examples:
Input : x = 9, n = 10
Output : -5.1463
Input : x = 5, n = 15
Output : 0.2837
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Simple approach :
We use two nested loops to compute factorial and use power function to compute power.
# Python3 code to get the sum of the series
import math
# Function to get the series
def Series( x , n ):
sum = 1
term = 1
y = 2
# Sum of n-1 terms starting from 2nd term
for i in range(1,n):
fct = 1
for j in range(1,y+1):
fct = fct * j
term = term * (-1)
m = term * math.pow(x, y) / fct
sum = sum + m
y += 2
return sum
# Driver Code
x = 9
n = 10
print('%.4f'% Series(x, n))
# This code is contributed by "Sharad_Bhardwaj".
Output:
-5.1463
Efficient approach :
We can avoid inner loop and use of power function by using values computed in previous iteration.
// C++ program to get the sum of the series
#include <math.h>
#include <stdio.h>
// Function to get the series
double Series(double x, int n)
{
double sum = 1, term = 1, fct = 1, p = 1, multi = 1;
// Computing sum of remaining n-1 terms.
for (int i = 1; i < n; i++) {
fct = fct * multi * (multi+1);
p = p*x*x;
term = (-1) * term;
multi += 2;
sum = sum + (term * p)/fct;
}
return sum;
}
// Driver Code
int main()
{
double x = 9;
int n = 10;
printf("%.4f", Series(x, n));
return 0;
}
Output:
-5.1463
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
Program for sum of cosh(x) series upto Nth term
Find the Nth term of the series where each term