Sum of the Tan(x) expansion upto N terms
Given two integers N and X. The task is to find the sum of tan(x) series up to N terms.
The series :
x + x3/3 + 2x5/15 + 17x7/315 + 62x9/2835……..
Examples:
Input : N = 6, X = 1
Output :The value from the expansion is 1.55137626113259Input : N = 4, X = 2
Output :The value from the expansion is 1.52063492063426
Approach :
The expansion of tan(x) is shown here. Compute the each term using a simple loops and get the required answer.
C++
// CPP program to find tan(x) expansion #include <bits/stdc++.h> using namespace std; // Function to find factorial of a number int fac(int num) { if (num == 0) return 1; // To store factorial of a number int fact = 1; for (int i = 1; i <= num; i++) fact = fact * i; // Return the factorial of a number return fact; } // Function to find tan(x) upto n terms void Tanx_expansion(int terms, int x) { // To store value of the expansion double sum = 0; for (int i = 1; i <= terms; i += 1) { // This loops here calculate Bernoulli number // which is further used to get the coefficient // in the expansion of tan x double B = 0; int Bn = 2 * i; for (int k = 0; k <= Bn; k++) { double temp = 0; for (int r = 0; r <= k; r++) temp = temp + pow(-1, r) * fac(k) * pow(r, Bn) / (fac(r) * fac(k - r)); B = B + temp / ((double)(k + 1)); } sum = sum + pow(-4, i) * (1 - pow(4, i)) * B * pow(x, 2 * i - 1) / fac(2 * i); } // Print the value of expansion cout << setprecision(10) << sum; } // Driver code int main() { int n = 6, x = 1; // Function call Tanx_expansion(n, x); return 0; } |
Java
// Java program to find tan(x) expansion class GFG { // Function to find factorial of a number static int fac(int num) { if (num == 0) return 1; // To store factorial of a number int fact = 1; for (int i = 1; i <= num; i++) fact = fact * i; // Return the factorial of a number return fact; } // Function to find tan(x) upto n terms static void Tanx_expansion(int terms, int x) { // To store value of the expansion double sum = 0; for (int i = 1; i <= terms; i += 1) { // This loops here calculate Bernoulli number // which is further used to get the coefficient // in the expansion of tan x double B = 0; int Bn = 2 * i; for (int k = 0; k <= Bn; k++) { double temp = 0; for (int r = 0; r <= k; r++) temp = temp + Math.pow(-1, r) * fac(k) * Math.pow(r, Bn) / (fac(r) * fac(k - r)); B = B + temp / ((double)(k + 1)); } sum = sum + Math.pow(-4, i) * (1 - Math.pow(4, i)) * B * Math.pow(x, 2 * i - 1) / fac(2 * i); } // Print the value of expansion System.out.printf("%.9f", sum); } // Driver code public static void main(String[] args) { int n = 6, x = 1; // Function call Tanx_expansion(n, x); } } // This code is contributed by Rajput-Ji |
C#
// C# program to find tan(x) expansion
using System;
class GFG
{
// Function to find factorial of a number
static int fac(int num)
{
if (num == 0)
return 1;
// To store factorial of a number
int fact = 1;
for (int i = 1; i <= num; i++)
fact = fact * i;
// Return the factorial of a number
return fact;
}
// Function to find tan(x) upto n terms
static void Tanx_expansion(int terms, int x)
{
// To store value of the expansion
double sum = 0;
for (int i = 1; i <= terms; i += 1)
{
// This loop here calculates
// Bernoulli number which is
// further used to get the coefficient
// in the expansion of tan x
double B = 0;
int Bn = 2 * i;
for (int k = 0; k <= Bn; k++)
{
double temp = 0;
for (int r = 0; r <= k; r++)
temp = temp + Math.Pow(-1, r) * fac(k) *
Math.Pow(r, Bn) / (fac(r) *
fac(k - r));
B = B + temp / ((double)(k + 1));
}
sum = sum + Math.Pow(-4, i) *
(1 - Math.Pow(4, i)) * B *
Math.Pow(x, 2 * i - 1) / fac(2 * i);
}
// Print the value of expansion
Console.Write("{0:F9}", sum);
}
// Driver code
public static void Main(String[] args)
{
int n = 6, x = 1;
// Function call
Tanx_expansion(n, x);
}
}
// This code is contributed by 29AjayKumar
[tabbyending]
1.551373344
Recommended Posts:
- Sum of the series (1*2) + (2*3) + (3*4) + ...... upto n terms
- Sum of the series 0.7, 0.77, 0.777, ... upto n terms
- Sum of series 2/3 - 4/5 + 6/7 - 8/9 + ------- upto n terms
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + - - - - - - upto n terms.
- Find sum of the series ?3 + ?12 +......... upto N terms
- Find the sum of series 0.X + 0.XX + 0.XXX +... upto k terms
- Find the sum of series 3, -6, 12, -24 . . . upto N terms
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ..... upto n terms
- Program to find the sum of the series 23+ 45+ 75+..... upto N terms
- Sum of Fibonacci numbers at even indexes upto N terms
- Find the sum of the series 1+11+111+1111+..... upto n terms
- Find sum of the series 1+22+333+4444+...... upto n terms
- Number of quadruples where the first three terms are in AP and last three terms are in GP
- Find the ln(X) and log10X with the help of expansion
- Program to print binomial expansion series
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



