Roots of Unity
Last Updated :
01 Sep, 2021
Given a small integer n, print all the n’th roots of unity up to 6 significant digits. We basically need to find all roots of equation xn – 1.
Examples:
Input : n = 1
Output : 1.000000 + i 0.000000
x - 1 = 0 , has only one root i.e., 1
Input : 2
Output : 1.000000 + i 0.000000
-1.000000 + i 0.000000
x2 - 1 = 0 has 2 distinct roots, i.e., 1 and -1
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 the program for the same.
C++
#include <bits/stdc++.h>
using namespace std;
void printRoots( int n)
{
double theta = M_PI*2/n;
for ( int k=0; k<n; k++)
{
double real = cos (k*theta);
double img = sin (k*theta);
printf ( "%.6f" , real);
img >= 0? printf ( " + i " ): printf ( " - i " );
printf ( "%.6f\n" , abs (img));
}
}
int main()
{
printRoots(1);
cout << endl;
printRoots(2);
cout << endl;
printRoots(3);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void printRoots( int n)
{
double theta = 3.14 * 2 /n;
for ( int k= 0 ; k<n; k++)
{
double real = Math.cos(k*theta);
double img = Math.sin(k*theta);
System.out.println(real);
if (img >= 0 )
System.out.println( " + i " );
else
System.out.println( " - i " );
System.out.println(Math.abs(img));
}
}
public static void main (String[] args)
{
printRoots( 1 );
printRoots( 2 );
printRoots( 3 );
}
}
|
Python3
import math
def printRoots(n):
theta = math.pi * 2 / n
for k in range ( 0 , n):
real = math.cos(k * theta)
img = math.sin(k * theta)
print (real, end = " " )
if (img > = 0 ):
print ( " + i " , end = " " )
else :
print ( " - i " , end = " " )
print ( abs (img))
if __name__ = = '__main__' :
printRoots( 1 )
printRoots( 2 )
printRoots( 3 )
|
C#
using System;
class GFG {
static void printRoots( int n)
{
double theta = 3.14*2/n;
for ( int k=0; k<n; k++)
{
double real = Math.Cos(k*theta);
double img = Math.Sin(k*theta);
Console.Write(real);
if (img >= 0)
Console.Write( " + i " );
else
Console.Write( " - i " );
Console.WriteLine(Math.Abs(img));
}
}
static void Main()
{
printRoots(1);
printRoots(2);
printRoots(3);
}
}
|
PHP
<?php
function printRoots( $n )
{
$theta = pi() * 2 / $n ;
for ( $k = 0; $k < $n ; $k ++)
{
$real = cos ( $k * $theta );
$img = sin( $k * $theta );
print ( round ( $real , 6));
$img >= 0 ? print ( " + i " ): print ( " - i " );
printf( round ( abs ( $img ), 6) . "\n" );
}
}
printRoots(1);
printRoots(2);
printRoots(3);
?>
|
Javascript
<script>
function printRoots(n)
{
var theta = (3.14*2/n);
for (k = 0; k < n; k++)
{
var real = Math.cos(k*theta);
var img = Math.sin(k*theta);
document.write(real.toFixed(6));
if (img >= 0)
document.write( " + i " );
else
document.write( " - i " );
document.write(Math.abs(img).toFixed(6)+'<br> ');
}
}
// Driver function to check the program
printRoots(1);
//document.write(' <br> ');
printRoots(2);
//document.write(' <br>');
printRoots(3);
</script>
|
Output:
1.000000 + i 0.000000
1.000000 + i 0.000000
-1.000000 + i 0.000000
1.000000 + i 0.000000
-0.500000 + i 0.866025
-0.500000 - i 0.866025
References: Wikipedia
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...