Program to Find the value of cos(nΘ)
Given a value of cos(Θ) and a variable
. The task is to find the value of cos(nΘ) using propety of trignomertric functions.
Note: n <= 15.
Examples:
Input : cos(Θ) = 0.5, n = 10 Output : -0.5 Input :cos(Θ) = 0.5, n = 3 Output : -0.995523
The problem can be solved using De moivre’s theorem and Binomial theorem as described below:
Using De-Moivre’s theorem, we have:

Now, as the value of both sin(Θ) and cos(Θ) is known. Put the values in above equation to get the answer.
Below is the implementation of the above idea:
C++
// CPP program to find the value of cos(n-theta) #include <bits/stdc++.h> #define MAX 16 using namespace std; int nCr[MAX][MAX] = { 0 }; // Fucntion to calculate the binomial // cofficient upto 15 void binomial() { // use simple DP to find cofficient 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 cos(n-theta) double findCosnTheta(double cosTheta, int n) { // find sinTheta from cosTheta double sinTheta = sqrt(1 - cosTheta * cosTheta); // to store required answer double ans = 0; // use to toggle sign in sequence. int toggle = 1; for (int i = 0; i <= n; i += 2) { ans = ans + nCr[n][i] * pow(cosTheta, n - i) * pow(sinTheta, i) * toggle; toggle = toggle * -1; } return ans; } // Driver code int main() { binomial(); double cosTheta = 0.5; int n = 10; cout << findCosnTheta(cosTheta, n) << endl; return 0; } |
Java
// Java program to find the value of cos(n-theta) class GFG { static int MAX=16; static int[][] nCr=new int[MAX][MAX]; // Fucntion to calculate the binomial // cofficient upto 15 static void binomial() { // use simple DP to find cofficient 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 cos(n-theta) static double findCosnTheta(double cosTheta, int n) { // find sinTheta from cosTheta double sinTheta = Math.sqrt(1 - cosTheta * cosTheta); // to store required answer double ans = 0; // use to toggle sign in sequence. int toggle = 1; for (int i = 0; i <= n; i += 2) { ans = ans + nCr[n][i] * Math.pow(cosTheta, n - i) * Math.pow(sinTheta, i) * toggle; toggle = toggle * -1; } return ans; } // Driver code public static void main(String[] args) { binomial(); double cosTheta = 0.5; int n = 10; System.out.println(String.format("%.5f",findCosnTheta(cosTheta, n))); } } // This code is contributed by mits |
Python3
# Python3 program to find the value of cos(n-theta) import math MAX=16nCr=[[0 for i in range(MAX)] for i in range(MAX)] # Fucntion to calculate the binomial # cofficient upto 15 def binomial(): # use simple DP to find cofficient 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 findCosnTheta(cosTheta,n): # find sinTheta from cosTheta sinTheta = math.sqrt(1 - cosTheta * cosTheta) # to store the required answer ans = 0 # use to toggle sign in sequence. toggle = 1 for i in range(0,n+1,2): ans = ans + nCr[n][i]*(cosTheta**(n - i)) *(sinTheta**i) * toggle toggle = toggle * -1 return ans # Driver code if __name__=='__main__': binomial() cosTheta = 0.5 n = 10 print(findCosnTheta(cosTheta, n)) # this code is contributed by sahilshelangia |
C#
// C# program to find the value of cos(n-theta) using System; public class GFG{ static int MAX=16; static int [,]nCr=new int[MAX,MAX]; // Fucntion to calculate the binomial // cofficient upto 15 static void binomial() { // use simple DP to find cofficient 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 cos(n-theta) static double findCosnTheta(double cosTheta, int n) { // find sinTheta from cosTheta double sinTheta = Math.Sqrt(1 - cosTheta * cosTheta); // to store required answer double ans = 0; // use to toggle sign in sequence. int toggle = 1; for (int i = 0; i <= n; i += 2) { ans = ans + nCr[n,i] * Math.Pow(cosTheta, n - i) * Math.Pow(sinTheta, i) * toggle; toggle = toggle * -1; } return ans; } // Driver code public static void Main() { binomial(); double cosTheta = 0.5; int n = 10; Console.WriteLine(findCosnTheta(cosTheta, n)); } } // This code is contributed by 29AjayKumar |
-0.5
Recommended Posts:
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- Program to find Sum of the series 1*3 + 3*5 + ....
- Program to find HCF iteratively
- Program to find the last two digits of x^y
- Program to find last two digits of 2^n
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n
- Program to find the sum of a Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)
- Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find the value of tan(nΘ)
- Program to find the value of sin(nΘ)
- Program to find sum of the given sequence
- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
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.



