Sum of N terms in the expansion of Arcsin(x)
Last Updated :
03 May, 2023
Given two integers N and X, the task is to find the value of Arcsin(x) using expansion upto N terms.
Examples:
Input: N = 4, X = 0.5
Output: 0.5233863467
Sum of first 4 terms in the expansion of Arcsin(x) for
x = 0.5 is 0.5233863467.
Input: N = 8, X = -0.5
Output: -0.5233948501
Approach: The expansion of arcsin(x) is given by :
Note: |x| < 1
The above expansion is solved by using two variables maintaining the numerator and the denominator.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void find_Solution( double x, int n)
{
double sum = x, e = 2, o = 1, p = 1;
for ( int i = 2; i <= n; i++) {
p += 2;
sum += ( double )(o / e) * ( double )( pow (x, p) / p);
o = o * (o + 2);
e = e * (e + 2);
}
cout << setprecision(10) << sum;
}
int main()
{
double x = -0.5;
if ( abs (x) >= 1) {
cout << "Invalid Input\n" ;
return 0;
}
int n = 8;
find_Solution(x, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void find_Solution( double x, int n)
{
double sum = x, e = 2 , o = 1 , p = 1 ;
for ( int i = 2 ; i <= n; i++)
{
p += 2 ;
sum += ( double )(o / e) *
( double )(Math.pow(x, p) / p);
o = o * (o + 2 );
e = e * (e + 2 );
}
System.out.println (sum);
}
public static void main (String[] args)
{
double x = - 0.5 ;
if (Math.abs(x) >= 1 )
{
System.out.println ( "Invalid Input" );
}
int n = 8 ;
find_Solution(x, n);
}
}
|
Python3
def find_Solution(x, n):
Sum = x
e = 2
o = 1
p = 1
for i in range ( 2 , n + 1 ):
p + = 2
Sum + = (o / e) * ( pow (x, p) / p)
o = o * (o + 2 )
e = e * (e + 2 )
print ( round ( Sum , 10 ))
x = - 0.5
if ( abs (x) > = 1 ):
print ( "Invalid Input\n" )
n = 8
find_Solution(x, n)
|
C#
using System;
class GFG
{
static void find_Solution( double x, int n)
{
double sum = x, e = 2, o = 1, p = 1;
for ( int i = 2; i <= n; i++)
{
p += 2;
sum += ( double )(o / e) *
( double )(Math.Pow(x, p) / p);
o = o * (o + 2);
e = e * (e + 2);
}
Console.WriteLine(sum);
}
public static void Main (String[] args)
{
double x = -0.5;
if (Math.Abs(x) >= 1)
{
Console.WriteLine( "Invalid Input" );
}
int n = 8;
find_Solution(x, n);
}
}
|
Javascript
<script>
function find_Solution(x, n)
{
let sum = x, e = 2, o = 1, p = 1;
for (let i = 2; i <= n; i++) {
p += 2;
sum += (o / e) * (Math.pow(x, p) / p);
o = o * (o + 2);
e = e * (e + 2);
}
document.write(sum.toFixed(10));
}
let x = -0.5;
if (Math.abs(x) >= 1) {
document.write( "Invalid Input<br>" );
}
let n = 8;
find_Solution(x, n);
</script>
|
Time Complexity: O(n), where ‘n’ is the number of terms in the Taylor series approximation.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...