设z是一个复数。如果对于某个正整数z,则n是第n个本原根。

对于任何正整数k < n

。
编写一个完整的程序或函数,以正整数n作为输入,输出所有的单元原根。您可以以极形式输出它们(e^θi或e^iθ,参数应该是小数,至少小数点2位)或矩形形式(a + bi或类似的形式,实部和虚部也应该是小数),它们可以以语言的列表/数组格式输出,也可以以字符串的形式输出,用空格或换行符分隔数字。计算统一的第n根或统一的第n个原始根的内置是不允许的。
这是密码-高尔夫,所以以字节为单位的最短代码将获胜。
6 -> e^1.05i, e^-1.05i # polar form
3 -> e^2.094395i, e^-2.094395i # any number of decimal places is OK as long as there are more than 2
8 -> 0.707 + 0.707i, 0.707 - 0.707i, -0.707 + 0.707i, -0.707 - 0.707i # rectangular form
1 -> 1 + 0i # this is OK
1 -> 1 # this is also OK
4 -> 0 + i, 0 - i # this is OK
4 -> i, -i # this is also OK发布于 2016-01-18 08:52:15
n->cis(360deg2rad(filter(k->gcd(k,n)<2,1:n))/n)这是一个lambda函数,它接受一个整数并返回一个复杂浮点数数组。若要调用它,请将其赋值给变量。它使用的方法与丹尼斯的果冻答案相同。
未高尔夫球:
function f(n::Int)
# Get the set of all k < n : gcd(k,n) = 1
K = filter(k -> gcd(k,n) < 2, 1:n)
# Convert these to radian measures
θ = deg2rad(K)
# Multiply by 360, divide by n
θ = 360 * θ / n
# Compute e^iz for all elements z of θ
return cis(θ)
end发布于 2016-01-18 15:17:21
:1-tGYf1X-!\Xpg)2j*YP*G/Ze!使用释放(9.3.1),它比此挑战更早。
在网上试试!
(在线编译器使用较新的版本,但代码在9.3.1版中运行,并给出了相同的结果)
有三个主要步骤:
0,1,.,N-1,对应于所有根。N的素因子分解来识别的。代码:
:1- % 1. Implicit input "N". Produce vector [0,1,...,N-1]
t % duplicate
GYf % 2. Prime factors of N
1X- % remove factor "1" if present (only if N==1)
!\ % all combinations of [0,1,...,N-1] modulo prime factors of N
Xpg % logical "and" along the prime-factor dimension
) % index into original vector [0,1,...,N-1] to keep only primitive roots
2j*YP*G/Ze % 3. Imaginary exponential to produce those roots
! % transpose for better output format发布于 2016-01-18 15:24:09
这是一个非“高尔夫语言”实现的郭炳泉's果冻答案。
->n{(1..n).map{|j|1i**(4.0*j/n)if j.gcd(n)<2}}未高尔夫球:
def r(n)
(1..n).each do |j|
if j.gcd(n) == 1 # if j is coprime with n, then this will be a primitive root of unity
p 1i**(4.0*j/n) # print the fourth power of i**(j/n), i.e. the root of unity
end
end
endhttps://codegolf.stackexchange.com/questions/69574
复制相似问题