Roots of unity: Difference between revisions

From Rosetta Code
Content added Content deleted
(page creation)
 
(→‎{{header|J}}: format fiddling)
Line 8: Line 8:
=={{header|J}}==
=={{header|J}}==


rou=: [: ^ i. * (o.0j2) % ]
rou=: [: ^ i. * (o. 0j2) % ]
rou 4
rou 4

Revision as of 10:18, 13 December 2007

Task
Roots of unity
You are encouraged to solve this task according to the task description, using any language you may know.

The purpose of this task is to explore working with complex numbers. Given n , find the n-th roots of unity.

J

   rou=: [: ^ i. * (o. 0j2) % ]

   rou 4
1 0j1 _1 0j_1

   rou 5
1 0.309017j0.951057 _0.809017j0.587785 _0.809017j_0.587785 0.309017j_0.951057

The computation can also be written as a loop, shown here for comparison only.

rou1=: 3 : 0
 z=. 0 $ r=. ^ o. 0j2 % y [ e=. 1
 for. i.y do.
  z=. z,e
  e=. e*r
 end.
 z
)