Unity 的根源

給定一個小整數 n,打印所有單位的第 n 個根,最多 6 位有效數字。我們基本上需要找到方程 x 的所有根n– 1.

例子: 

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 

如果任何復數在提高到某個冪時得到 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 the
equation 

z^n  = 1
or , 
z^n - 1 = 0

我們可以在這里使用 De Moivre 公式 

( 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

利用上述事實,我們可以輕松地打印出所有 n 次單位根!

以下是相同的程序。

// 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\n", abs(img));
	}
}


// Driver function to check the program
int main()
{
	printRoots(1);
	cout << endl;
	printRoots(2);
	cout << endl;
	printRoots(3);
	return 0;
}


登錄后免費查看全文
立即登錄
App下載
技術鄰APP
工程師必備
  • 項目客服
  • 培訓客服
  • 平臺客服

TOP

2
1
1