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++ implementation of the approach #include <bits/stdc++.h> using namespace std;
// Function to find the arcsin(x) void find_Solution( double x, int n)
{ double sum = x, e = 2, o = 1, p = 1;
for ( int i = 2; i <= n; i++) {
// The power to which 'x' is raised
p += 2;
sum += ( double )(o / e) * ( double )( pow (x, p) / p);
// Numerator value
o = o * (o + 2);
// Denominator value
e = e * (e + 2);
}
cout << setprecision(10) << sum;
} // Driver code 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 implementation of the approach import java.io.*;
class GFG
{ // Function to find the arcsin(x) static void find_Solution( double x, int n)
{ double sum = x, e = 2 , o = 1 , p = 1 ;
for ( int i = 2 ; i <= n; i++)
{
// The power to which 'x' is raised
p += 2 ;
sum += ( double )(o / e) *
( double )(Math.pow(x, p) / p);
// Numerator value
o = o * (o + 2 );
// Denominator value
e = e * (e + 2 );
}
System.out.println (sum);
} // Driver code 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);
} } // This code is contributed by ajit |
# Python3 implementation of the approach # Function to find the arcsin(x) def find_Solution(x, n):
Sum = x
e = 2
o = 1
p = 1
for i in range ( 2 , n + 1 ):
# The power to which 'x' is raised
p + = 2
Sum + = (o / e) * ( pow (x, p) / p)
# Numerator value
o = o * (o + 2 )
# Denominator value
e = e * (e + 2 )
print ( round ( Sum , 10 ))
# Driver code x = - 0.5
if ( abs (x) > = 1 ):
print ( "Invalid Input\n" )
n = 8
find_Solution(x, n) # This code is contributed by Mohit Kumar |
// C# implementation of the approach using System;
class GFG
{ // Function to find the arcsin(x) static void find_Solution( double x, int n)
{ double sum = x, e = 2, o = 1, p = 1;
for ( int i = 2; i <= n; i++)
{
// The power to which 'x' is raised
p += 2;
sum += ( double )(o / e) *
( double )(Math.Pow(x, p) / p);
// Numerator value
o = o * (o + 2);
// Denominator value
e = e * (e + 2);
}
Console.WriteLine(sum);
} // Driver code 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);
} } // This code is contributed by PrinciRaj1992 |
<script> // JavaScript implementation of the approach // Function to find the arcsin(x) function find_Solution(x, n)
{ let sum = x, e = 2, o = 1, p = 1;
for (let i = 2; i <= n; i++) {
// The power to which 'x' is raised
p += 2;
sum += (o / e) * (Math.pow(x, p) / p);
// Numerator value
o = o * (o + 2);
// Denominator value
e = e * (e + 2);
}
document.write(sum.toFixed(10));
} // Driver code let x = -0.5;
if (Math.abs(x) >= 1) {
document.write( "Invalid Input<br>" );
}
let n = 8;
find_Solution(x, n);
// This code is contributed by Surbhi Tyagi. </script> |
Output:
-0.5233948501
Time Complexity: O(n), where ‘n’ is the number of terms in the Taylor series approximation.
Auxiliary Space: O(1)