给定两个整数N和X ,任务是使用扩展到N个项来找到Arcsin(x)的值。
例子:
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
方法: arcsin(x)的扩展由下式给出:
注意: | x | <1
通过使用两个保留分子和分母的变量来解决上述扩展问题。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
//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
# 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#
// 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
Javascript
输出:
-0.5233948501