团结的根源

给定一个小整数n,打印所有的n个单位根,最多6个有效数字。我们基本上需要找到方程x的所有根 N – 1.

null

例如:

Input :  n = 1Output : 1.000000 + i 0.000000x - 1 = 0 , has only one root i.e., 1Input :  2Output : 1.000000 + i 0.000000    -1.000000 + i 0.000000x2 - 1 = 0 has 2 distinct roots, i.e., 1 and -1 

如果任何复数被提升到某个幂次时为1,则称其为单位根。 单位的n根是任何复数,当提升到n的幂时,它给出1。

Mathematically, An nth root of unity, where n is a positive integer (i.e. n = 1, 2, 3, …) is a number z satisfying theequation z^n  = 1or , z^n - 1 = 0

我们可以使用 德莫伊夫公式 在这里

( Cos x + i Sin x )^k = Cos kx + i Sin kxSetting 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

利用以上事实,我们可以很容易地打印出统一的第n个根!

下面是同样的程序。

C++

// C++ program to print n'th roots of unity
#include <bits/stdc++.h>
using namespace std;
// 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" , abs (img));
}
}
// Driver function to check the program
int main()
{
printRoots(1);
cout << endl;
printRoots(2);
cout << endl;
printRoots(3);
return 0;
}


JAVA

// Java program to print n'th roots of unity
import java.io.*;
class GFG {
// This function receives an integer n , and prints
// all the nth roots of unity
static void printRoots( int n)
{
// theta = 2*pi/n
double theta = 3.14 * 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 = Math.cos(k*theta);
double img = Math.sin(k*theta);
// Print real and imaginary parts
System.out.println(real);
if (img >= 0 )
System.out.println( " + i " );
else
System.out.println( " - i " );
System.out.println(Math.abs(img));
}
}
// Driver function to check the program
public static void main (String[] args)
{
printRoots( 1 );
//System.out.println();
printRoots( 2 );
//System.out.println();
printRoots( 3 );
}
}
// This code is contributed by Raj


Python3

# Python3 program to print n'th roots of unity
import math
# This function receives an integer n , and prints
# all the nth roots of unity
def printRoots(n):
# theta = 2*pi/n
theta = math.pi * 2 / n
# print all nth roots with 6 significant digits
for k in range ( 0 , n):
# calculate the real and imaginary part of root
real = math.cos(k * theta)
img = math.sin(k * theta)
# Print real and imaginary parts
print (real, end = " " )
if (img > = 0 ):
print ( " + i " , end = " " )
else :
print ( " - i " , end = " " )
print ( abs (img))
# Driver function to check the program
if __name__ = = '__main__' :
printRoots( 1 )
printRoots( 2 )
printRoots( 3 )
# This code is contributed by
# Sanjit_Prasad


C#

// C# program to print n'th roots of unity
using System;
class GFG {
// This function receives an integer n , and prints
// all the nth roots of unity
static void printRoots( int n)
{
// theta = 2*pi/n
double theta = 3.14*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 = Math.Cos(k*theta);
double img = Math.Sin(k*theta);
// Print real and imaginary parts
Console.Write(real);
if (img >= 0)
Console.Write( " + i " );
else
Console.Write( " - i " );
Console.WriteLine(Math.Abs(img));
}
}
// Driver function to check the program
static void Main()
{
printRoots(1);
printRoots(2);
printRoots(3);
}
}
// This code is contributed by mits


PHP

<?php
// PHP program to print n'th roots of unity
// This function receives an integer n,
// and prints all the nth roots of unity
function printRoots( $n )
{
// theta = 2*pi/n
$theta = pi() * 2 / $n ;
// print all nth roots with 6
// significant digits
for ( $k = 0; $k < $n ; $k ++)
{
// calculate the real and imaginary
// part of root
$real = cos ( $k * $theta );
$img = sin( $k * $theta );
// Print real and imaginary parts
print ( round ( $real , 6));
$img >= 0 ? print ( " + i " ): print ( " - i " );
printf( round ( abs ( $img ), 6) . "" );
}
}
// Driver Code
printRoots(1);
printRoots(2);
printRoots(3);
// This code is contributed by mits
?>


Javascript

<script>
// javascript program to print n'th roots of unity
// This function receives an integer n , and prints
// all the nth roots of unity
function printRoots(n)
{
// theta = 2*pi/n
var theta = (3.14*2/n);
// print all nth roots with 6 significant digits
for (k = 0; k < n; k++)
{
// calculate the real and imaginary part of root
var real = Math.cos(k*theta);
var img = Math.sin(k*theta);
// Print real and imaginary parts
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);
// This code is contributed by shikhasingrajput
</script>


输出:

1.000000 + i 0.0000001.000000 + i 0.000000-1.000000 + i 0.0000001.000000 + i 0.000000-0.500000 + i 0.866025-0.500000 - i 0.866025

参考资料: 维基百科

本文由 阿什图什·库马尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享