ユニティのプリミティブルーツ


11

ましょうz複素数になります。zは、特定の正の整数および任意の正の整数の場合、単一性の n番目のプリミティブルートですn k < n

チャレンジ

n入力として正の整数を指定すると、ユニティのn番目のプリミティブルートをすべて出力する完全なプログラムまたは関数を記述します。それらを極形式(e^θiまたはe^iθ、引数は小数点以下2桁以上の小数でなければなりません)または長方形形式(a + biまたは同様の形式、実数部と虚数部も小数である必要があります)で出力でき、言語のリストに出力できます/ array形式、またはスペースまたは改行で区切られた数字の文字列として。ユニティの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

+ -iはz ^ 8 = 1の解ではありませんか?
RosLuP

回答:


9

ゼリー、11 9バイト

-2バイトの@Dennisに感謝します!

Rg=1O÷H-*

私は、1からNまでのすべての根の集合の差を折り畳むことにより、Nと互いに素な数を生成したかったのですが、どのように@Dennisの方法を使用したのかわかりませんでした。

Rg=1O÷H-*         Monadic chain:          6
R                 Range                   [1,2,3,4,5,6]
 g                Hook gcds with range    [1,2,3,2,1,6]
  =1              [gcds equal to one]     [1,0,0,0,1,0]
    O             Replicate indices       [1,5]
     ÷H           Divide by half of N     [1/3,5/3]
       -          Numeric literal: - by itself is -1.
        *         Take -1 to those powers [cis π/3,cis 5π/3]

ここで試してみてくださいこのバージョンのJellyで有効ですが、2016年2月1日以降のバージョンにはない場合があります。


4

ゼリー、14 バイト

Rg=1O°÷×ı360Æe

オンラインでお試しください!

使い方

z = e2tπiは、ある整数kに対してt = k / nの場合にのみ、1のn 番目のルートです。

zは、knが互いに素である場合にのみプリミティブです。

Rg=1O°÷×ı360Æe  Main link. Input: n

R               Yield [1, ..., n].
 g              Compute the GCDs of reach integer and n.
  =1            Compare the GCDs with 1.
    O           Get all indices of 1's.
                This computes all the list of all k in [1, ..., n] 
                such that k and n are coprime.
     °          Convert the integers to radians.
      ÷         Divide the results by n.
       ×ı360    Multiply the quotient by the imaginary number 360i.
            Æe  Map exp over the results.

2

ジュリア、48バイト

n->cis(360deg2rad(filter(k->gcd(k,n)<2,1:n))/n)

これは、整数を受け入れ、複素数の浮動小数点の配列を返すラムダ関数です。呼び出すには、変数に割り当てます。デニスのゼリーの回答と同じアプローチを使用します。

ゴルフをしていない:

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

2

ルビー、46バイト

これは、Thomas KwaのJelly回答の非「ゴルフ言語」実装です。

->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
end

2

MATL、27バイト

:1-tGYf1X-!\Xpg)2j*YP*G/Ze!

この課題よりも前のリリース(9.3.1)を使用します。

オンラインでお試しください!

(オンラインコンパイラは新しいリリースを使用しますが、コードはリリース9.3.1で実行され、同じ結果が得られます)

説明

3つの主な手順があります。

  1. すべてのルートに対応する整数01...、を生成しますN-1
  2. プリミティブルートに対応する整数のみを保持します。これらは、の素因数分解を使用して識別されNます。
  3. 虚指数で実際の根を生成します。

コード:

: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

1

Matlab 49バイト

n=input('');q=0:n-1;exp(i*2*pi/n.*q(gcd(n,q)==1))

初めてタスクを取得できませんでしたが、今はここにあります。出力は次のとおりです。

6
ans =
    0.5000 + 0.8660i   0.5000 - 0.8660i

3
答えには、原始的なものだけでなく、統一のすべてのルートが表示されます。
フレイ

@flawr発言ありがとう、最初はタスクを取得できませんでした。ソリューションを編集しました
-brainkz

1

ES6、96バイト

n=>[...Array(n).keys()].filter(i=>g(i,n)<2,g=(a,b)=>a?g(b%a,a):b).map(i=>'e^'+Math.PI*2*i/n+'i')

極形状は最も短い出力でした。


1

PARI / GP、41バイト

かなり簡単です:nと互いに素である1からnまでの数を見つけてから、

n->[exp(2*Pi*I*m/n)|m<-[1..n],gcd(n,m)<2]

もっと短い方法が必要ですが、これは私が見つけることができる最高のものでした。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.