-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathRoots of Unity
66 lines (49 loc) · 1.5 KB
/
Roots of Unity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Any complex number is said to be root of unity if it gives 1 when raised to some power.
nth root of unity is any complex number such that it gives 1 when raised to the power n.
Mathematically,
An nth root of unity, where n is a positive integer
(i.e. n = 1, 2, 3, …) is a number z satisfying the
equation
z^n = 1
or ,
z^n - 1 = 0
We can use the De Moivre’s formula here.
( Cos x + i Sin x )^k = Cos kx + i Sin kx
Setting x = 2*pi/n, we can obtain all the nth roots
of unity, using the fact that Nth roots are set of
numbers given by,
Cos (2*pi*k/n) + i Sin(2*pi*k/n)
Where, 0 <= k < n
Using the above fact we can easily print all the nth roots of unity !
Below is a C program for the same.
#include<stdio.h>
#include <math.h>
// A C program to print n'th roots of unity
// This function receives an integer n , and prints
// all the nth roots of unity
void printRoots(int n)
{
// theta = 2*pi/n
double theta = M_PI*2/n;
// print all nth roots with 6 significant digits
for(int k=0; k<n; k++)
{
// calculate the real and imaginary part of root
double real = cos(k*theta);
double img = sin(k*theta);
// Print real and imaginary parts
printf("%.6f", real);
img >= 0? printf(" + i "): printf(" - i ");
printf("%.6f\n", fabs(img));
}
}
// Driver function to check the program
int main()
{
printRoots(1);
printf("\n");
printRoots(2);
printf("\n");
printRoots(3);
return 0;
}