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
Simple approach :
We use two nested loops to compute factorial and use power function to compute power.
C++
#include <bits/stdc++.h>
using namespace std;
double Series( double x, int n)
{
double sum = 1, term = 1, fct, j, y = 2, m;
int i;
for (i = 1; i < n; i++) {
fct = 1;
for (j = 1; j <= y; j++) {
fct = fct * j;
}
term = term * (-1);
m = term * pow (x, y) / fct;
sum = sum + m;
y += 2;
}
return sum;
}
int main()
{
double x = 9;
int n = 10;
cout << Series(x, n);
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
double Series( double x, int n)
{
double sum = 1, term = 1, fct, j, y = 2, m;
int i;
for (i = 1; i < n; i++) {
fct = 1;
for (j = 1; j <= y; j++) {
fct = fct * j;
}
term = term * (-1);
m = term * pow (x, y) / fct;
sum = sum + m;
y += 2;
}
return sum;
}
int main()
{
double x = 9;
int n = 10;
printf ( "%.4f" , Series(x, n));
return 0;
}
|
Java
import java.io.*;
class MathSeries {
static double Series( double x, int n)
{
double sum = 1 , term = 1 , fct, j, y = 2 , m;
int i;
for (i = 1 ; i < n; i++) {
fct = 1 ;
for (j = 1 ; j <= y; j++) {
fct = fct * j;
}
term = term * (- 1 );
m = Math.pow(x, y) / fct;
m = m * term;
sum = sum + m;
y += 2 ;
}
return sum;
}
public static void main(String[] args)
{
double x = 3 ;
int n = 4 ;
System.out.println(Math.round(Series(x, n) *
10000.0 ) / 10000.0 );
}
}
|
Python3
import math
def Series( x , n ):
sum = 1
term = 1
y = 2
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
x = 9
n = 10
print ( '%.4f' % Series(x, n))
|
C#
using System;
class GFG {
static double Series( double x, int n)
{
double sum = 1, term = 1, fct, j, y = 2, m;
int i;
for (i = 1; i < n; i++) {
fct = 1;
for (j = 1; j <= y; j++) {
fct = fct * j;
}
term = term * (-1);
m = Math.Pow(x, y) / fct;
m = m * term;
sum = sum + m;
y += 2;
}
return sum;
}
public static void Main()
{
double x = 9;
int n = 10;
Console.Write(Series(x, n) *
10000.0 / 10000.0);
}
}
|
PHP
<?php
function Series( $x , $n )
{
$sum = 1; $term = 1;
$fct ; $j ; $y = 2; $m ;
for ( $i = 1; $i < $n ; $i ++)
{
$fct = 1;
for ( $j = 1; $j <= $y ; $j ++)
{
$fct = $fct * $j ;
}
$term = $term * (-1);
$m = $term * pow( $x , $y ) / $fct ;
$sum = $sum + $m ;
$y += 2;
}
return $sum ;
}
$x = 9;
$n = 10;
$precision = 4;
echo substr (number_format(Series( $x , $n ),
$precision + 1, '.' , '' ), 0, -1);
?>
|
Javascript
<script>
function Series(x, n)
{
let sum = 1, term = 1, fct, j, y = 2, m;
let i;
for (i = 1; i < n; i++)
{
fct = 1;
for (j = 1; j <= y; j++)
{
fct = fct * j;
}
term = term * (-1);
m = term * Math.pow(x, y) / fct;
sum = sum + m;
y += 2;
}
return sum;
}
let x = 9;
let n = 10;
document.write(Series(x, n).toFixed(4));
</script>
|
Time Complexity: O(n * ylogny)
Auxiliary Space: O(1), since no extra space has been taken.
Efficient approach :
We can avoid inner loop and use of power function by using values computed in previous iteration.
C++
#include <math.h>
#include <stdio.h>
double Series( double x, int n)
{
double sum = 1, term = 1, fct = 1, p = 1, multi = 1;
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;
}
int main()
{
double x = 9;
int n = 10;
printf ( "%.4f" , Series(x, n));
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <limits.h>
double Series( double x, int n)
{
double sum = 1, term = 1, fct = 1, p = 1, multi = 1;
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;
}
int main()
{
double x = 9;
int n = 10;
printf ( "%.4f" , Series(x, n));
return 0;
}
|
Java
import java.io.*;
class GFG {
static double Series( double x, int n)
{
double sum = 1 , term = 1 , fct = 1 ;
double p = 1 , multi = 1 ;
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;
}
public static void main(String args[])
{
double x = 9 ;
int n = 10 ;
System.out.printf( "%.4f" , Series(x, n));
}
}
|
Python3
def Series(x, n):
sum = 1
term = 1
fct = 1
p = 1
multi = 1
for i in range ( 1 , n):
fct = fct * multi * (multi + 1 )
p = p * x * x
term = ( - 1 ) * term
multi + = 2
sum = sum + (term * p) / fct
return sum
x = 9
n = 10
print ( '%.4f' % Series(x, n))
|
C#
using System;
class GFG {
static float Series( double x, int n)
{
double sum = 1, term = 1, fct = 1;
double p = 1, multi = 1;
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 ( float )sum;
}
public static void Main()
{
double x = 9;
int n = 10;
Console.Write(Series(x, n));
}
}
|
PHP
<?php
function Series( $x , $n )
{
$sum = 1; $term = 1; $fct = 1;
$p = 1; $multi = 1;
for ( $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 ;
}
$x = 9;
$n = 10;
$precision = 4;
echo substr (number_format(Series( $x , $n ),
$precision + 1, '.' , '' ), 0, -1);
?>
|
Javascript
<script>
function Series(x , n) {
var sum = 1, term = 1, fct = 1;
var p = 1, multi = 1;
for (let 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;
}
var x = 9;
var n = 10;
document.write(Series(x, n).toFixed(4));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1), since no extra space has been taken.
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 :
16 Aug, 2022
Like Article
Save Article