Suma de la expansión Tan(x) hasta N términos

Dados dos enteros N y X . La tarea es encontrar la suma de la serie tan(x) hasta N términos.
Las series : 
 

x + x 3 /3 + 2x 5 /15 + 17x 7 /315 + 62x 9 /2835……..

Ejemplos: 
 

Entrada: N = 6, X = 1 
Salida: El valor de la expansión es 1,55137626113259
Entrada: N = 4, X = 2 
Salida: El valor de la expansión es 1,52063492063426

Enfoque:  Aquí
se muestra la expansión de tan(x) . Calcule cada término usando bucles simples y obtenga la respuesta requerida. 
 

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

Python3

# Python3 program to find tan(x) expansion
 
# Function to find factorial of a number
def fac(num):
    if (num == 0):
        return 1;
 
    # To store factorial of a number
    fact = 1;
    for i in range(1, num + 1):
        fact = fact * i;
 
    # Return the factorial of a number
    return fact;
 
# Function to find tan(x) upto n terms
def Tanx_expansion(terms, x):
 
    # To store value of the expansion
    sum = 0;
 
    for i in range(1, terms + 1):
 
        # This loops here calculate Bernoulli number
        # which is further used to get the coefficient
        # in the expansion of tan x
        B = 0;
        Bn = 2 * i;
        for k in range(Bn + 1):
            temp = 0;
            for r in range(0, k + 1):
                temp = temp + pow(-1, r) * fac(k) \
                    * pow(r, Bn) / (fac(r) * fac(k - r));
 
            B = B + temp / ((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
    print("%.9f" %(sum));
 
# Driver code
if __name__ == '__main__':
    n, x = 6, 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

Javascript

<script>
 
// JavaScript program to find tan(x) expansion
 
// Function to find factorial of a number
function fac(num)
{
    if (num == 0)
        return 1;
 
    // To store factorial of a number
    let fact = 1;
    for (let i = 1; i <= num; i++)
        fact = fact * i;
 
    // Return the factorial of a number
    return fact;
}
 
// Function to find tan(x) upto n terms
function Tanx_expansion(terms, x)
{
    // To store value of the expansion
    let sum = 0;
 
    for (let 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
        let B = 0;
        let Bn = 2 * i;
        for (let k = 0; k <= Bn; k++) {
            let temp = 0;
            for (let 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 / (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
    document.write(sum.toFixed(10));
}
 
// Driver code
 
    let n = 6, x = 1;
 
    // Function call
    Tanx_expansion(n, x);
 
// This code is contributed by Surbhi Tyagi.
 
</script>
Producción: 

1.551373344

 

Complejidad temporal: O(n 2 )

Espacio Auxiliar: O(1), ya que no se ha ocupado ningún espacio extra.

Publicación traducida automáticamente

Artículo escrito por ManishKhetan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *