Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmProgram to find the value of tan(nΘ)

Program to find the value of tan(nΘ)

Given a value of tan(?) and a variable n <=15. The task is to find the value of tan(n?) using property of trigonometric functions.
Examples
 

Input: tan(?) = 0.3, n = 10
Output: -2.15283

Input: tan(?) = 0.3, n = 5
Output: 0.37293

 

The problem can be solved using De moivre’s theorem and Binomial theorem as described below:
Using De-Moivre’s theorem, we have

    \begin{align*} \cos(n\theta)+\iota \sin(n\theta)&=(\cos \theta+\iota \sin \theta)^n\\ &=\cos^n \theta +\binom{n}{1} \cos^{n-1} \theta (\iota \sin \theta)+\binom{n}{2} \cos^{n-2} \theta (\iota \sin \theta)^2+\\ & \binom{n}{3} \cos^{n-3} \theta (\iota \sin \theta)^3+ \cdots \\ \end{align*} Equating the result to real part to get the value of $\cos n\theta$ finally, we got-\\ $\cos (n \theta)=\binom{n}{0}\cos^{n}\theta \sin^0 \theta-\binom{n}{2}\cos^{n-2}\theta \sin^2 \theta+\binom{n}{4}\cos^{n-4}\theta \sin^4 \theta- \cdots$ \\ Equating the result to imaginary part to get the value of $\sin n\theta$ finally, we got-\\ $\sin (n \theta)=\binom{n}{1}\cos^{n-1}\theta \sin^1 \theta-\binom{n}{3}\cos^{n-3}\theta \sin^3 \theta+\binom{n}{5}\cos^{n-5}\theta \sin^5 \theta- \cdots$ \\ \\ As we know, $$\tan n\theta=\frac{\sin n\theta}{\cos n\theta}$$ \\ Put the value of $\sin n\theta $ and $\cos n\theta$, We get\\ $$\tan n\theta=\frac{\binom{n}{1}\cos^{n-1}\theta \sin^1 \theta-\binom{n}{3}\cos^{n-3}\theta \sin^3 \theta+\binom{n}{5}\cos^{n-5}\theta \sin^5 \theta- \cdots}{\binom{n}{0}\cos^{n}\theta \sin^0 \theta-\binom{n}{2}\cos^{n-2}\theta \sin^2 \theta+\binom{n}{4}\cos^{n-4}\theta \sin^4 \theta- \cdots}$$ \\ Dividing numerator and denomenator by $\cos^n \theta$\\ $$\tan n\theta=\frac{\binom{n}{1}\tan^1 \theta-\binom{n}{3}\tan^3 \theta+\binom{n}{5}\tan^5 \theta-\cdots}{1-\binom{n}{2}\tan^2 \theta+\binom{n}{4}\tan^4 \theta-\binom{n}{6}\tan^6 \theta+\cdots}$$ As we have value of $\tan \theta$, \\ Put in final equation-

Below is the implementation of above approach: 
 

C++




// C++ program to find the value
// of cos(n-theta)
 
#include <bits/stdc++.h>
#define ll long long int
#define MAX 16
using namespace std;
ll nCr[MAX][MAX] = { 0 };
 
// This function use to calculate the
// binomial coefficient upto 15
void binomial()
{
 
    // use simple DP to find coefficient
    for (int i = 0; i < MAX; i++) {
        for (int j = 0; j <= i; j++) {
            if (j == 0 || j == i)
                nCr[i][j] = 1;
            else
                nCr[i][j] = nCr[i - 1][j] + nCr[i - 1][j - 1];
        }
    }
}
 
// Function to find the value of
double findTanNTheta(double tanTheta, ll n)
{
    // store required answer
    double ans = 0, numerator = 0, denominator = 0;
 
    // use to toggle sign in sequence.
    ll toggle = 1;
 
    // calculate numerator
    for (int i = 1; i <= n; i += 2) {
        numerator = numerator + nCr[n][i] * pow(tanTheta, i) * toggle;
        toggle = toggle * -1;
    }
 
    // calculate denominator
    denominator = 1;
    toggle = -1;
 
    for (int i = 2; i <= n; i += 2) {
        numerator = numerator + nCr[n][i] * pow(tanTheta, i) * toggle;
        toggle = toggle * -1;
    }
 
    ans = numerator / denominator;
 
    return ans;
}
 
// Driver code.
int main()
{
    binomial();
    double tanTheta = 0.3;
 
    ll n = 10;
 
    cout << findTanNTheta(tanTheta, n) << endl;
    return 0;
}


Java




// Java program to find the value
// of cos(n-theta)
public class GFG {
     
    private static final int MAX = 16 ;
    static long nCr[][] = new long [MAX][MAX] ;
 
    // This function use to calculate the
    // binomial coefficient upto 15
    static void binomial()
    {
 
        // use simple DP to find coefficient
        for (int i = 0; i < MAX; i++) {
            for (int j = 0; j <= i; j++) {
                if (j == 0 || j == i)
                    nCr[i][j] = 1;
                else
                    nCr[i][j] = nCr[i - 1][j] + nCr[i - 1][j - 1];
            }
        }
    }
 
    // Function to find the value of
    static double findTanNTheta(double tanTheta, int n)
    {
        // store required answer
        double ans = 0, numerator = 0, denominator = 0;
     
        // use to toggle sign in sequence.
        long toggle = 1;
     
        // calculate numerator
        for (int i = 1; i <= n; i += 2) {
            numerator = numerator + nCr[n][i] * Math.pow(tanTheta, i) * toggle;
            toggle = toggle * -1;
        }
     
        // calculate denominator
        denominator = 1;
        toggle = -1;
     
        for (int i = 2; i <= n; i += 2) {
            numerator = numerator + nCr[n][i] * Math.pow(tanTheta, i) * toggle;
            toggle = toggle * -1;
        }
     
        ans = numerator / denominator;
     
        return ans;
    }
 
    // Driver code
    public static void main(String args[])
    {
        binomial();
        double tanTheta = 0.3;
        int n = 10;
        System.out.println(findTanNTheta(tanTheta, n));
    }
// This code is contributed by ANKITRAI1
}


Python3




# Python3 program to find the value
# of cos(n-theta)
 
import math
MAX=16
nCr=[[0 for i in range(MAX)] for i in range(MAX)]
 
# Function to calculate the binomial
# coefficient upto 15
def binomial():
     
    # use simple DP to find coefficient
    for i in range(MAX):
        for j in range(0,i+1):
            if j == 0 or j == i:
                nCr[i][j] = 1
            else:
                nCr[i][j] = nCr[i - 1][j] + nCr[i - 1][j - 1]
 
# Function to find the value of cos(n-theta)
def findTanNTheta(tanTheta,n):
     
    # store required answer
    numerator=0
    denominator=1
     
    # to store required answer
    ans = 0
     
    # use to toggle sign in sequence.
    toggle = 1
 
    # calculate numerator
    for i in range(1,n+1,2):
        numerator = (numerator + nCr[n][i]*
                    (tanTheta**(i)) * toggle)
        toggle = toggle * -1
    # calculate denominator
    toggle=-1
    for i in range(2,n+1,2):
        numerator = (numerator + nCr[n][i]*
                    (tanTheta**i) * toggle)
        toggle = toggle * -1
    ans=numerator/denominator
    return ans
 
     
# Driver code
if __name__=='__main__':
    binomial()
    tanTheta = 0.3
    n = 10
    print(findTanNTheta(tanTheta, n))
     
# this code is contributed by sahilshelangia


C#




// C# program to find the value
// of cos(n-theta)
 
using System;
public class GFG {
      
    private static int MAX = 16 ;
    static long[,] nCr = new long [MAX,MAX] ;
  
    // This function use to calculate the
    // binomial coefficient upto 15
    static void binomial()
    {
  
        // use simple DP to find coefficient
        for (int i = 0; i < MAX; i++) {
            for (int j = 0; j <= i; j++) {
                if (j == 0 || j == i)
                    nCr[i,j] = 1;
                else
                    nCr[i,j] = nCr[i - 1,j] + nCr[i - 1,j - 1];
            }
        }
    }
  
    // Function to find the value of
    static double findTanNTheta(double tanTheta, int n)
    {
        // store required answer
        double ans = 0, numerator = 0, denominator = 0;
      
        // use to toggle sign in sequence.
        long toggle = 1;
      
        // calculate numerator
        for (int i = 1; i <= n; i += 2) {
            numerator = numerator + nCr[n,i] * Math.Pow(tanTheta, i) * toggle;
            toggle = toggle * -1;
        }
      
        // calculate denominator
        denominator = 1;
        toggle = -1;
      
        for (int i = 2; i <= n; i += 2) {
            numerator = numerator + nCr[n,i] * Math.Pow(tanTheta, i) * toggle;
            toggle = toggle * -1;
        }
      
        ans = numerator / denominator;
      
        return ans;
    }
  
    // Driver code
    public static void Main()
    {
        binomial();
        double tanTheta = 0.3;
        int n = 10;
        Console.Write(findTanNTheta(tanTheta, n));
    }
 
}


PHP




<?php
// PHP program to find the value
// of cos(n-theta)
 
$MAX=16;
$nCr=array_fill(0, $MAX, array_fill(0, $MAX, 0));
 
// This function use to calculate the
// binomial coefficient upto 15
function binomial()
{
global $MAX,$nCr;
    // use simple DP to find coefficient
    for ($i = 0; $i < $MAX; $i++) {
        for ($j = 0; $j <= $i; $j++) {
            if ($j == 0 || $j == $i)
                $nCr[$i][$j] = 1;
            else
                $nCr[$i][$j] = $nCr[$i - 1][$j] + $nCr[$i - 1][$j - 1];
        }
    }
}
 
// Function to find the value of
function findTanNTheta($tanTheta, $n)
{
    global $MAX,$nCr;
    // store required answer
    $ans = 0;
    $numerator = 0;
    $denominator = 0;
 
    // use to toggle sign in sequence.
    $toggle = 1;
 
    // calculate numerator
    for ($i = 1; $i <= $n; $i += 2) {
        $numerator = $numerator + $nCr[$n][$i] * pow($tanTheta, $i) * $toggle;
        $toggle = $toggle * -1;
    }
 
    // calculate denominator
    $denominator = 1;
    $toggle = -1;
 
    for ($i = 2; $i <= $n; $i += 2) {
        $numerator = $numerator + $nCr[$n][$i] * pow($tanTheta, $i) * $toggle;
        $toggle = $toggle * -1;
    }
 
    $ans = $numerator / $denominator;
 
    return $ans;
}
 
// Driver code.
 
    binomial();
    $tanTheta = 0.3;
 
    $n = 10;
 
    echo findTanNTheta($tanTheta, $n);
     
// This code is contributed by mits
?>


Javascript




<script>
 
// Javascript program to find the value
// of cos(n-theta)
 
MAX = 16
var nCr = Array.from(Array(MAX), ()=> new Array(MAX));
 
// This function use to calculate the
// binomial coefficient upto 15
function binomial()
{
 
    // use simple DP to find coefficient
    for (var i = 0; i < MAX; i++) {
        for (var j = 0; j <= i; j++) {
            if (j == 0 || j == i)
                nCr[i][j] = 1;
            else
                nCr[i][j] = nCr[i - 1][j] +
                nCr[i - 1][j - 1];
        }
    }
}
 
// Function to find the value of
function findTanNTheta(tanTheta, n)
{
    // store required answer
    ans = 0, numerator = 0, denominator = 0;
 
    // use to toggle sign in sequence.
    toggle = 1;
 
    // calculate numerator
    for (var i = 1; i <= n; i += 2) {
        numerator = numerator + nCr[n][i] *
        Math.pow(tanTheta, i) * toggle;
        toggle = toggle * -1;
    }
 
    // calculate denominator
    denominator = 1;
    toggle = -1;
 
    for (var i = 2; i <= n; i += 2) {
        numerator = numerator + nCr[n][i] *
        Math.pow(tanTheta, i) * toggle;
        toggle = toggle * -1;
    }
 
    ans = numerator / denominator;
 
    return ans.toFixed(5);
}
 
// Driver code.
binomial();
var tanTheta = 0.3;
var n = 10;
document.write( findTanNTheta(tanTheta, n));
 
</script>


Output: 

-2.15283

 

Time Complexity: O(n + MAX2)

Auxiliary Space: O(MAX2)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments