Roots of unity

From Rosetta Code
Revision as of 05:15, 23 July 2024 by Chkas (talk | contribs) (→‎{{header|EasyLang}})
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.


Task

Given   n,   find the   nth   roots of unity.

11l

F polar(r, theta)
   R r * (cos(theta) + sin(theta) * 1i)

F croots(n)
   R (0 .< n).map(k -> polar(1, 2 * k * math:pi / @n))

L(nr) 2..10
   print(nr‘ ’croots(nr))
Output:
2 [1, -1]
3 [1, -0.5+0.866025404i, -0.5-0.866025404i]
4 [1, 1i, -1, -1i]
5 [1, 0.309016994+0.951056516i, -0.809016994+0.587785252i, -0.809016994-0.587785252i, 0.309016994-0.951056516i]
6 [1, 0.5+0.866025404i, -0.5+0.866025404i, -1, -0.5-0.866025404i, 0.5-0.866025404i]
7 [1, 0.623489802+0.781831482i, -0.222520934+0.974927912i, -0.900968868+0.433883739i, -0.900968868-0.433883739i, -0.222520934-0.974927912i, 0.623489802-0.781831482i]
8 [1, 0.707106781+0.707106781i, 1i, -0.707106781+0.707106781i, -1, -0.707106781-0.707106781i, -1i, 0.707106781-0.707106781i]
9 [1, 0.766044443+0.64278761i, 0.173648178+0.984807753i, -0.5+0.866025404i, -0.939692621+0.342020143i, -0.939692621-0.342020143i, -0.5-0.866025404i, 0.173648178-0.984807753i, 0.766044443-0.64278761i]
10 [1, 0.809016994+0.587785252i, 0.309016994+0.951056516i, -0.309016994+0.951056516i, -0.809016994+0.587785252i, -1, -0.809016994-0.587785252i, -0.309016994-0.951056516i, 0.309016994-0.951056516i, 0.809016994-0.587785252i]

Ada

with Ada.Text_IO;                 use Ada.Text_IO;
with Ada.Float_Text_IO;           use Ada.Float_Text_IO;
with Ada.Numerics.Complex_Types;  use Ada.Numerics.Complex_Types;

procedure Roots_Of_Unity is
   Root : Complex;
begin
   for N in 2..10 loop
      Put_Line ("N =" & Integer'Image (N));
      for K in 0..N - 1 loop
         Root :=
             Compose_From_Polar
             (  Modulus  => 1.0,
                Argument => Float (K),
                Cycle    => Float (N)
             );
            -- Output
         Put ("   k =" & Integer'Image (K) & ", ");
         if Re (Root) < 0.0 then
            Put ("-");
         else
            Put ("+");
         end if;
         Put (abs Re (Root), Fore => 1, Exp => 0);
         if Im (Root) < 0.0 then
            Put ("-");
         else
            Put ("+");
         end if;
         Put (abs Im (Root), Fore => 1, Exp => 0);
         Put_Line ("i");
      end loop;
   end loop;
end Roots_Of_Unity;

Ada provides a direct implementation of polar composition of complex numbers x ei y. The function Compose_From_Polar is used to compose roots. The third argument of the function is the cycle. Instead of the standard cycle 2π, N is used. Sample output:

N = 2
   k = 0, +1.00000+0.00000i
   k = 1, -1.00000+0.00000i
N = 3
   k = 0, +1.00000+0.00000i
   k = 1, -0.50000+0.86603i
   k = 2, -0.50000-0.86603i
N = 4
   k = 0, +1.00000+0.00000i
   k = 1, +0.00000+1.00000i
   k = 2, -1.00000+0.00000i
   k = 3, +0.00000-1.00000i
N = 5
   k = 0, +1.00000+0.00000i
   k = 1, +0.30902+0.95106i
   k = 2, -0.80902+0.58779i
   k = 3, -0.80902-0.58779i
   k = 4, +0.30902-0.95106i
N = 6
   k = 0, +1.00000+0.00000i
   k = 1, +0.50000+0.86603i
   k = 2, -0.50000+0.86603i
   k = 3, -1.00000+0.00000i
   k = 4, -0.50000-0.86603i
   k = 5, +0.50000-0.86603i
N = 7
   k = 0, +1.00000+0.00000i
   k = 1, +0.62349+0.78183i
   k = 2, -0.22252+0.97493i
   k = 3, -0.90097+0.43388i
   k = 4, -0.90097-0.43388i
   k = 5, -0.22252-0.97493i
   k = 6, +0.62349-0.78183i
N = 8
   k = 0, +1.00000+0.00000i
   k = 1, +0.70711+0.70711i
   k = 2, +0.00000+1.00000i
   k = 3, -0.70711+0.70711i
   k = 4, -1.00000+0.00000i
   k = 5, -0.70711-0.70711i
   k = 6, +0.00000-1.00000i
   k = 7, +0.70711-0.70711i
N = 9
   k = 0, +1.00000+0.00000i
   k = 1, +0.76604+0.64279i
   k = 2, +0.17365+0.98481i
   k = 3, -0.50000+0.86603i
   k = 4, -0.93969+0.34202i
   k = 5, -0.93969-0.34202i
   k = 6, -0.50000-0.86603i
   k = 7, +0.17365-0.98481i
   k = 8, +0.76604-0.64279i
N = 10
   k = 0, +1.00000+0.00000i
   k = 1, +0.80902+0.58779i
   k = 2, +0.30902+0.95106i
   k = 3, -0.30902+0.95106i
   k = 4, -0.80902+0.58779i
   k = 5, -1.00000+0.00000i
   k = 6, -0.80902-0.58779i
   k = 7, -0.30902-0.95106i
   k = 8, +0.30902-0.95106i
   k = 9, +0.80902-0.58779i

ALGOL 68

Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny
FORMAT complex fmt=$g(-6,4)"⊥"g(-6,4)$;
FOR root FROM 2 TO 10 DO
  printf(($g(4)$,root));
  FOR n FROM 0 TO root-1 DO
    printf(($xf(complex fmt)$,complex exp( 0 I 2*pi*n/root)))
  OD;
  printf($l$)
OD

Output:

  +2 1.0000⊥0.0000 -1.000⊥0.0000
  +3 1.0000⊥0.0000 -.5000⊥0.8660 -.5000⊥-.8660
  +4 1.0000⊥0.0000 0.0000⊥1.0000 -1.000⊥0.0000 -.0000⊥-1.000
  +5 1.0000⊥0.0000 0.3090⊥0.9511 -.8090⊥0.5878 -.8090⊥-.5878 0.3090⊥-.9511
  +6 1.0000⊥0.0000 0.5000⊥0.8660 -.5000⊥0.8660 -1.000⊥0.0000 -.5000⊥-.8660 0.5000⊥-.8660
  +7 1.0000⊥0.0000 0.6235⊥0.7818 -.2225⊥0.9749 -.9010⊥0.4339 -.9010⊥-.4339 -.2225⊥-.9749 0.6235⊥-.7818
  +8 1.0000⊥0.0000 0.7071⊥0.7071 0.0000⊥1.0000 -.7071⊥0.7071 -1.000⊥0.0000 -.7071⊥-.7071 -.0000⊥-1.000 0.7071⊥-.7071
  +9 1.0000⊥0.0000 0.7660⊥0.6428 0.1736⊥0.9848 -.5000⊥0.8660 -.9397⊥0.3420 -.9397⊥-.3420 -.5000⊥-.8660 0.1736⊥-.9848 0.7660⊥-.6428
 +10 1.0000⊥0.0000 0.8090⊥0.5878 0.3090⊥0.9511 -.3090⊥0.9511 -.8090⊥0.5878 -1.000⊥0.0000 -.8090⊥-.5878 -.3090⊥-.9511 0.3090⊥-.9511 0.8090⊥-.5878

Arturo

rect: function [r,phi][
    to :complex @[ r * cos phi, r * sin phi ]
]
roots: function [n][
    map 0..dec n 'k -> rect 1.0 2 * k * pi / n
]

loop 2..10 'nr ->
    print [pad to :string nr 3 "=>" join.with:", " to [:string] .format:".3f" roots nr]
Output:
  2 => 1.000+0.000i, -1.000+0.000i 
  3 => 1.000+0.000i, -0.500+0.866i, -0.500-0.866i 
  4 => 1.000+0.000i, 0.000+1.000i, -1.000+0.000i, -0.000-1.000i 
  5 => 1.000+0.000i, 0.309+0.951i, -0.809+0.588i, -0.809-0.588i, 0.309-0.951i 
  6 => 1.000+0.000i, 0.500+0.866i, -0.500+0.866i, -1.000+0.000i, -0.500-0.866i, 0.500-0.866i 
  7 => 1.000+0.000i, 0.623+0.782i, -0.223+0.975i, -0.901+0.434i, -0.901-0.434i, -0.223-0.975i, 0.623-0.782i 
  8 => 1.000+0.000i, 0.707+0.707i, 0.000+1.000i, -0.707+0.707i, -1.000+0.000i, -0.707-0.707i, -0.000-1.000i, 0.707-0.707i 
  9 => 1.000+0.000i, 0.766+0.643i, 0.174+0.985i, -0.500+0.866i, -0.940+0.342i, -0.940-0.342i, -0.500-0.866i, 0.174-0.985i, 0.766-0.643i 
 10 => 1.000+0.000i, 0.809+0.588i, 0.309+0.951i, -0.309+0.951i, -0.809+0.588i, -1.000+0.000i, -0.809-0.588i, -0.309-0.951i, 0.309-0.951i, 0.809-0.588i

ATS

I compute the roots of unity by the formula exp(-2*pi*k*sqrt(-1)/n), k = 0, 1, ..., n-1. Most of the code is to build part of a general infrastructure for supporting standard C complex types.

(ATS code looks like ML code, and has a very unusual and relatively strict type system, but semantically it is essentially C.)

(*

This program has to be compiled without -std=c99, which patscc will
insert unless you override the setting.

##myatsccdef=\
patscc \
  --gline \
  -atsccomp gcc \
  -I"${PATSHOME}" \
  -I"${PATSHOME}/ccomp/runtime" \
  -L"${PATSHOME}/ccomp/atslib/lib" \
  -DATS_MEMALLOC_LIBC \
  -o $fname($1) $1 -lm

*)

(*

  I use the C _Complex types, but not the newer _Imaginary types.
  Thus I demonstrate how one might add new floating point types
  nicely.

  (In my opinion, it is good to use m4 or similar tools when writing
  such repetitive code. Doing so reduces both work and the frequency
  of errors. Also you could then more easily add support for the many
  extension types such as "_Float128 complex" (quadruple precision).
  
  One could, of course, define one's own complex types directly in
  ATS.

*)

#include "share/atspre_staload.hats"

#define ATS_EXTERN_PREFIX "myatspre_"

#define NIL list_nil ()
#define ::  list_cons

(*------------------------------------------------------------------*)

%{^

#include <complex.h>

#define myatspre_inline ATSinline ()

typedef float complex myatstype_fcomplex;
typedef double complex myatstype_dcomplex;
typedef long double complex myatstype_lcomplex;

#define myatspre_CMPLXF CMPLXF
#define myatspre_CMPLX CMPLX
#define myatspre_CMPLXL CMPLXL

myatspre_inline atsvoid_t0ype
myatspre_fprint_fcomplex (atstype_ref r,
                          myatstype_fcomplex x)
{
  double rx = crealf (x);
  double ix = cimagf (x);
  const char *plus = (ix < 0) ? "" : "+";
  fprintf ((FILE *) r, "%f%s%fi", rx, plus, ix);
}

#define myatspre_print_fcomplex(x) myatspre_fprint_fcomplex (stdout, (x))
#define myatspre_prerr_fcomplex(x) myatspre_fprint_fcomplex (stderr, (x))

myatspre_inline atsvoid_t0ype
myatspre_fprint_dcomplex (atstype_ref r,
                          myatstype_dcomplex x)
{
  double rx = creal (x);
  double ix = cimag (x);
  const char *plus = (ix < 0) ? "" : "+";
  fprintf ((FILE *) r, "%f%s%fi", rx, plus, ix);
}

#define myatspre_print_dcomplex(x) myatspre_fprint_dcomplex (stdout, (x))
#define myatspre_prerr_dcomplex(x) myatspre_fprint_dcomplex (stderr, (x))

myatspre_inline atsvoid_t0ype
myatspre_fprint_lcomplex (atstype_ref r,
                          myatstype_lcomplex x)
{
  long double rx = creall (x);
  long double ix = cimagl (x);
  const char *plus = (ix < 0) ? "" : "+";
  fprintf ((FILE *) r, "%Lf%s%Lfi", rx, plus, ix);
}

#define myatspre_print_lcomplex(x) myatspre_fprint_lcomplex (stdout, (x))
#define myatspre_prerr_lcomplex(x) myatspre_fprint_lcomplex (stderr, (x))

myatspre_inline myatstype_fcomplex
myatspre_g0float_cmplx_float_fcomplex (atstype_float x,
                                       atstype_float y)
{
  return myatspre_CMPLXF (x, y);
}

myatspre_inline myatstype_dcomplex
myatspre_g0float_cmplx_double_dcomplex (atstype_double x,
                                        atstype_double y)
{
  return myatspre_CMPLX (x, y);
}

myatspre_inline myatstype_lcomplex
myatspre_g0float_cmplx_ldouble_lcomplex (atstype_ldouble x,
                                         atstype_ldouble y)
{
  return myatspre_CMPLXL (x, y);
}

myatspre_inline myatstype_fcomplex
myatspre_g0int2float_int_fcomplex (atstype_int x)
{
  return x;
}

myatspre_inline myatstype_dcomplex
myatspre_g0int2float_int_dcomplex (atstype_int x)
{
  return x;
}

myatspre_inline myatstype_lcomplex
myatspre_g0int2float_int_lcomplex (atstype_int x)
{
  return x;
}

myatspre_inline myatstype_fcomplex
myatspre_g0float_mul_fcomplex (myatstype_fcomplex x,
                               myatstype_fcomplex y)
{
  return x * y;
}

myatspre_inline myatstype_dcomplex
myatspre_g0float_mul_dcomplex (myatstype_dcomplex x,
                               myatstype_dcomplex y)
{
  return x * y;
}

myatspre_inline myatstype_lcomplex
myatspre_g0float_mul_lcomplex (myatstype_lcomplex x,
                               myatstype_lcomplex y)
{
  return x * y;
}

myatspre_inline myatstype_fcomplex
myatspre_exp_fcomplex (myatstype_fcomplex x)
{
  return cexpf (x);
}

myatspre_inline myatstype_dcomplex
myatspre_exp_dcomplex (myatstype_dcomplex x)
{
  return cexp (x);
}

myatspre_inline myatstype_lcomplex
myatspre_exp_lcomplex (myatstype_lcomplex x)
{
  return cexpl (x);
}

myatspre_inline myatstype_fcomplex
myatspre_pow_fcomplex (myatstype_fcomplex x,
                       myatstype_fcomplex y)
{
  return cpowf (x, y);
}

myatspre_inline myatstype_dcomplex
myatspre_pow_dcomplex (myatstype_dcomplex x,
                       myatstype_dcomplex y)
{
  return cpow (x, y);
}

myatspre_inline myatstype_lcomplex
myatspre_pow_lcomplex (myatstype_lcomplex x,
                       myatstype_lcomplex y)
{
  return cpowl (x, y);
}

%}

(*------------------------------------------------------------------*)

tkindef fcomplex_kind = "myatstype_fcomplex"
stadef fcmplxknd = fcomplex_kind
stadef fcomplex = g0float fcmplxknd

tkindef dcomplex_kind = "myatstype_dcomplex"
stadef dcmplxknd = dcomplex_kind
stadef dcomplex = g0float dcmplxknd

tkindef lcomplex_kind = "myatstype_lcomplex"
stadef lcmplxknd = lcomplex_kind
stadef lcomplex = g0float lcmplxknd

extern fn print_fcomplex : fcomplex -<1> void = "mac#%"
extern fn prerr_fcomplex : fcomplex -<1> void = "mac#%"
extern fn fprint_fcomplex : fprint_type fcomplex = "mac#%"
overload print with print_fcomplex
overload prerr with prerr_fcomplex
overload fprint with fprint_fcomplex
implement fprint_val<fcomplex> = fprint_fcomplex

extern fn print_dcomplex : dcomplex -<1> void = "mac#%"
extern fn prerr_dcomplex : dcomplex -<1> void = "mac#%"
extern fn fprint_dcomplex : fprint_type dcomplex = "mac#%"
overload print with print_dcomplex
overload prerr with prerr_dcomplex
overload fprint with fprint_dcomplex
implement fprint_val<dcomplex> = fprint_dcomplex

extern fn print_lcomplex : lcomplex -<1> void = "mac#%"
extern fn prerr_lcomplex : lcomplex -<1> void = "mac#%"
extern fn fprint_lcomplex : fprint_type lcomplex = "mac#%"
overload print with print_lcomplex
overload prerr with prerr_lcomplex
overload fprint with fprint_lcomplex
implement fprint_val<lcomplex> = fprint_lcomplex

extern fn g0int2float_int_fcomplex : int -<> fcomplex = "mac#%"
extern fn g0int2float_int_dcomplex : int -<> dcomplex = "mac#%"
extern fn g0int2float_int_lcomplex : int -<> lcomplex = "mac#%"
implement g0int2float<intknd,fcmplxknd> = g0int2float_int_fcomplex
implement g0int2float<intknd,dcmplxknd> = g0int2float_int_dcomplex
implement g0int2float<intknd,lcmplxknd> = g0int2float_int_lcomplex

extern fn g0float_cmplx_float_fcomplex : (float, float) -<> fcomplex = "mac#%"
extern fn g0float_cmplx_double_dcomplex : (double, double) -<> dcomplex = "mac#%"
extern fn g0float_cmplx_ldouble_lcomplex : (ldouble, ldouble) -<> lcomplex = "mac#%"
extern fn {tk2 : tkind} {tk1 : tkind} g0float_cmplx : (g0float tk1, g0float tk1) -<> g0float tk2
implement g0float_cmplx<fcmplxknd><fltknd> = g0float_cmplx_float_fcomplex
implement g0float_cmplx<dcmplxknd><dblknd> = g0float_cmplx_double_dcomplex
implement g0float_cmplx<lcmplxknd><ldblknd> = g0float_cmplx_ldouble_lcomplex
overload cmplx with g0float_cmplx

extern fn g0float_mul_fcomplex : g0float_aop_type fcmplxknd = "mac#%"
extern fn g0float_mul_dcomplex : g0float_aop_type dcmplxknd = "mac#%"
extern fn g0float_mul_lcomplex : g0float_aop_type lcmplxknd = "mac#%"
implement g0float_mul<fcmplxknd> = g0float_mul_fcomplex
implement g0float_mul<dcmplxknd> = g0float_mul_dcomplex
implement g0float_mul<lcmplxknd> = g0float_mul_lcomplex

(*------------------------------------------------------------------*)
(* Most "math" functions are not defined in the prelude. Here we will
   follow the conventions of libats/libc, which does not use the
   floating point typekinds. *)

staload "libats/libc/SATS/math.sats"
staload _ = "libats/libc/DATS/math.dats"

extern fn exp_fcomplex : fcomplex -<> fcomplex = "mac#%"
extern fn exp_dcomplex : dcomplex -<> dcomplex = "mac#%"
extern fn exp_lcomplex : lcomplex -<> lcomplex = "mac#%"
implement exp<fcomplex> = exp_fcomplex
implement exp<dcomplex> = exp_dcomplex
implement exp<lcomplex> = exp_lcomplex

extern fn pow_fcomplex : (fcomplex, fcomplex) -<> fcomplex = "mac#%"
extern fn pow_dcomplex : (dcomplex, dcomplex) -<> dcomplex = "mac#%"
extern fn pow_lcomplex : (lcomplex, lcomplex) -<> lcomplex = "mac#%"
implement pow<fcomplex> = pow_fcomplex
implement pow<dcomplex> = pow_dcomplex
implement pow<lcomplex> = pow_lcomplex

(*------------------------------------------------------------------*)

fn
nth_roots_of_unity
          {n : pos}
          (n : int n)
    :<> list (dcomplex, n) =
  let
    val C = cmplx (0.0, ~((2.0 * M_PI) / g0i2f n))

    fun
    loop {k : nat | k <= n}
         .<k>.
         (k     : int k,
          accum : list (dcomplex, n - k))
        :<> list (dcomplex, n) =
      if k = 0 then
        accum
      else
        loop (pred k, exp (g0i2f (pred k) * C) :: accum)
  in
    loop (n, NIL)
  end

fn
nth_powers {m   : int}
           {n   : pos}
           (lst : list (dcomplex, m),
            n   : int n)
    :<1> list (dcomplex, m) =
  let
    val nth : dcomplex = g0i2f n
    implement list_map$fopr<dcomplex><dcomplex> x = pow (x, nth)
  in
    list_vt2t (list_map<dcomplex><dcomplex> lst)
  end    

fn
show_results
          {n : pos}
          (n : int n)
    :<1> void =
  let
    val nth_roots = nth_roots_of_unity n
    val ones = nth_powers (nth_roots, n)
  in
    println! ();
    println! ("roots of unity = ", nth_roots);
    println! ("roots raised ", n, " = ", ones)
  end

fun
loop_over_args
          {argc   : int}
          {k      : pos | k <= argc}
          {p_args : addr}
          .<argc - k>.
          (pf_args : !array_v (string, p_args, argc) |
           argc    : int argc,
           p_args  : ptr p_args,
           k       : int k)
    :<1> void =
  if k <> argc then
    let
      macdef args = !p_args
      val argument = args[k]
      val n = $extfcall ([n : int] int n, "atoi", argument)
    in
      if 0 < n then
        show_results n;
      loop_over_args (pf_args | argc, p_args, succ k)
    end

implement
main0 {argc} (argc, argv) =
  let
    val [p_args : addr]
        @(pf_args, pf_minus | p_args) =
      argv_takeout_strarr {argc} argv

    val () = loop_over_args {argc} {1} {p_args}
                            (pf_args | argc, p_args, 1)

    prval () = minus_addback (pf_minus, pf_args | argv)
  in
    println! ()
  end

(*------------------------------------------------------------------*)
Output:
$ myatscc roots-of-unity.dats && ./roots-of-unity 1 2 3 4 5

roots of unity = 1.000000+0.000000i
roots raised 1 = 1.000000+0.000000i

roots of unity = 1.000000+0.000000i, -1.000000-0.000000i
roots raised 2 = 1.000000+0.000000i, 1.000000+0.000000i

roots of unity = 1.000000+0.000000i, -0.500000-0.866025i, -0.500000+0.866025i
roots raised 3 = 1.000000+0.000000i, 1.000000+0.000000i, 1.000000+0.000000i

roots of unity = 1.000000+0.000000i, 0.000000-1.000000i, -1.000000-0.000000i, -0.000000+1.000000i
roots raised 4 = 1.000000+0.000000i, 1.000000+0.000000i, 1.000000+0.000000i, 1.000000+0.000000i

roots of unity = 1.000000+0.000000i, 0.309017-0.951057i, -0.809017-0.587785i, -0.809017+0.587785i, 0.309017+0.951057i
roots raised 5 = 1.000000+0.000000i, 1.000000+0.000000i, 1.000000+0.000000i, 1.000000+0.000000i, 1.000000+0.000000i

AutoHotkey

ahk forum: discussion

n := 8, a := 8*atan(1)/n
Loop %n%
   i := A_Index-1, t .= cos(a*i) ((s:=sin(a*i))<0 ? " - i*" . -s : " + i*" . s) "`n"
Msgbox % t

AWK

# syntax: GAWK -f ROOTS_OF_UNITY.AWK
BEGIN {
    pi = 3.1415926
    for (n=2; n<=5; n++) {
      printf("%d: ",n)
      for (root=0; root<=n-1; root++) {
        real = cos(2 * pi * root / n)
        imag = sin(2 * pi * root / n)
        printf("%8.5f %8.5fi",real,imag)
        if (root != n-1) { printf(", ") }
      }
      printf("\n")
    }
    exit(0)
}
Output:
2:  1.00000  0.00000i, -1.00000  0.00000i
3:  1.00000  0.00000i, -0.50000  0.86603i, -0.50000 -0.86603i
4:  1.00000  0.00000i,  0.00000  1.00000i, -1.00000  0.00000i, -0.00000 -1.00000i
5:  1.00000  0.00000i,  0.30902  0.95106i, -0.80902  0.58779i, -0.80902 -0.58779i,  0.30902 -0.95106i

BASIC

Works with: QBasic version 1.1
Works with: QuickBasic version 4.5
Translation of: Java

For high n's, this may repeat the root of 1 + 0*i.

 CLS
 PI = 3.1415926#
 n = 5 'this can be changed for any desired n
 angle = 0 'start at angle 0
 DO
 	real = COS(angle) 'real axis is the x axis
 	IF (ABS(real) < 10 ^ -5) THEN real = 0 'get rid of annoying sci notation
 	imag = SIN(angle) 'imaginary axis is the y axis
 	IF (ABS(imag) < 10 ^ -5) THEN imag = 0 'get rid of annoying sci notation
 	PRINT real; "+"; imag; "i" 'answer on every line
 	angle = angle + (2 * PI) / n
 'all the way around the circle at even intervals
 LOOP WHILE angle < 2 * PI

BASIC256

twopi = 2 * pi
n = 5

for m = 0 to n-1
	theta = m*twopi/n
	real = cos(theta)
	imag = sin(theta)
	if imag >= 0 then
		print ljust(string(real),9,"0"); " + "; ljust(string(imag),13,"0"); "i"
	else
		print ljust(string(real),9,"0"); " - "; ljust(string(-imag),13,"0"); "i"
	end if
next m

BBC BASIC

      @% = &20408
      FOR n% = 2 TO 5
        PRINT STR$(n%) ": " ;
        FOR root% = 0 TO n%-1
          real = COS(2*PI * root% / n%)
          imag = SIN(2*PI * root% / n%)
          PRINT real imag "i" ;
          IF root% <> n%-1 PRINT "," ;
        NEXT
        PRINT
      NEXT n%

Output:

2:   1.0000  0.0000i, -1.0000  0.0000i
3:   1.0000  0.0000i, -0.5000  0.8660i, -0.5000 -0.8660i
4:   1.0000  0.0000i,  0.0000  1.0000i, -1.0000  0.0000i, -0.0000 -1.0000i
5:   1.0000  0.0000i,  0.3090  0.9511i, -0.8090  0.5878i, -0.8090 -0.5878i,  0.3090 -0.9511i

Craft Basic

define theta = 0, real = 0, imag = 0
define pi = 3.14, n = 5

for m = 0 to n - 1

	let theta = m * (pi * 2) / n
	let real = cos(theta)
	let imag = sin(theta)

	if imag >= 0 then

		print real, comma, " ", imag, "i"

	else

		print real, comma, " ", imag * -1, "i"

	endif

	wait

next m
Output:

1, 0i 0.31, 0.95i -0.81, 0.59i -0.81, 0.59i 0.30, 0.95i

FreeBASIC

#define twopi 6.2831853071795864769252867665590057684

dim as uinteger m, n
dim as double real, imag, theta
input "n? ", n

for m = 0 to n-1
    theta = m*twopi/n
    real = cos(theta)
    imag = sin(theta)
    if imag >= 0 then
        print using "#.##### + #.##### i"; real; imag
    else
        print using "#.##### - #.##### i"; real; -imag
    end if
next m

FutureBasic

window 1, @"Roots of Unity", (0,0,1050,200)

long n, root
double real, imag

for n = 2 to 7
  print n;":" ;
  for root = 0 to n-1
    real = cos( 2 * pi * root / n)
    imag = sin( 2 * pi * root / n)
    print using "-##.#####"; real;using "-##.#####"; imag; "i";
    if root != n-1 then print ",";
  next
  print
next

HandleEvents

Output:

 2:  1.00000  0.00000i, -1.00000  0.00000i
 3:  1.00000  0.00000i, -0.50000  0.86603i, -0.50000 -0.86603i
 4:  1.00000  0.00000i,  0.00000  1.00000i, -1.00000  0.00000i, -0.00000 -1.00000i
 5:  1.00000  0.00000i,  0.30902  0.95106i, -0.80902  0.58779i, -0.80902 -0.58779i,  0.30902 -0.95106i
 6:  1.00000  0.00000i,  0.50000  0.86603i, -0.50000  0.86603i, -1.00000  0.00000i, -0.50000 -0.86603i,  0.50000 -0.86603i
 7:  1.00000  0.00000i,  0.62349  0.78183i, -0.22252  0.97493i, -0.90097  0.43388i, -0.90097 -0.43388i, -0.22252 -0.97493i,  0.62349 -0.78183i

Liberty BASIC

WindowWidth  =400
WindowHeight =400

'nomainwin

open "N'th Roots of One" for graphics_nsb_nf as #w

#w "trapclose [quit]"

for n =1 To 10
    angle =0
    #w "font arial 16 bold"
    print n; "th roots."
    #w "cls"
    #w "size 1 ; goto 200 200 ; down ; color lightgray ; circle 150 ; size 10 ; set 200 200 ; size 2"
    #w "up ; goto 200 0 ; down ; goto 200 400 ; up ; goto 0 200 ; down ; goto 400 200"
    #w "up ; goto 40 20 ; down ; color black"
    #w "font arial 6"
    #w "\"; n; " roots of 1."

    for i = 1 To n
        x = cos( Radian( angle))
        y = sin( Radian( angle))

        print using( "##", i); ":  ( " + using( "##.######", x);_
          " +i *" +using( "##.######", y); ")      or     e^( i *"; i -1; " *2 *Pi/ "; n; ")"

        #w "color "; 255 *i /n; " 0 "; 256 -255 *i /n
        #w "up ; goto 200 200"
        #w "down ; goto "; 200 +150 *x; " "; 200 -150 *y
        #w "up   ; goto "; 200 +165 *x; " "; 200 -165 *y
        #w "\"; str$( i)
        #w "up"

        angle =angle +360 /n

    next i

    timer 500, [on]
    wait
  [on]
    timer 0
next n

wait

[quit]
    close #w

    end

function Radian( theta)
    Radian =theta *3.1415926535 /180
end function

PureBasic

OpenConsole()
For n = 2 To 10
  angle = 0
  PrintN(Str(n))
  For i = 1 To n
    x.f = Cos(Radian(angle))    
    y.f = Sin(Radian(angle)) 
    PrintN( Str(i) + ":  " + StrF(x, 6) +  " / " + StrF(y, 6))
    angle = angle + (360 / n) 
  Next
Next
Input()

Run BASIC

PI = 3.1415926535
FOR n = 2 TO 5
  PRINT n;":" ;
   FOR root = 0 TO n-1
     real = COS(2*PI * root / n)
     imag = SIN(2*PI * root / n)
     PRINT using("-##.#####",real);using("-##.#####",imag);"i";
     IF root <> n-1 then PRINT "," ;
  NEXT
  PRINT
NEXT

Output:

2:   1.00000   0.00000i, -1.00000   0.00000i
3:   1.00000   0.00000i, -0.50000   0.86603i, -0.50000  -0.86603i
4:   1.00000   0.00000i,  0.00000   1.00000i, -1.00000   0.00000i,  0.00000  -1.00000i
5:   1.00000   0.00000i,  0.30902   0.95106i, -0.80902   0.58779i, -0.80902  -0.58779i,  0.30902  -0.95106i

TI-89 BASIC

cZeros(x^n - 1, x)

For n=3 in exact mode, the results are

{-1/2+√(3)/2*i, -1/2-√(3)/2*i, 1}

True BASIC

Translation of: BASIC
LET num_pi = 3.1415926
LET n = 5                         !this can be changed for any desired n
LET angle = 0                     !start at angle 0
DO
   LET real = COS(angle)          !real axis is the x axis
   IF (ABS(real) < 10^(-5)) THEN  !get rid of annoying sci notation
      LET real = 0
   END IF
   LET imag = SIN(angle)          !imaginary axis is the y axis
   IF (ABS(imag) < 10^(-5)) THEN  !get rid of annoying sci notation
      LET imag = 0
   END IF
   PRINT real; "+"; imag; "i"     !answer on every line
   LET angle = angle+(2*num_pi)/n
   !all the way around the circle at even intervals
LOOP WHILE angle < 2*num_pi
END

Yabasic

twopi = 2 * pi
n = 5

for m = 0 to n-1
    theta = m*twopi/n
    real = cos(theta)
    imag = sin(theta)
    if imag >= 0 then
        print real using("##.########"), " + ", imag using("#.########"), "i"
    else
        print real using("##.########"), " + ", -imag using("#.########"), "i"
    end if
next m


Ursala

The roots function takes a number n to the nth root of -1, squares it, and iteratively makes a list of its first n powers (oblivious to roundoff error). Complex functions cpow and mul are used, which are called from the host system's standard C library.

#import std
#import nat
#import flo

roots = ~&htxPC+ c..mul:-0^*DlSiiDlStK9\iota c..mul@iiX+ c..cpow/-1.+ div/1.+ float

#cast %jLL

tests = roots* <1,2,3,4,5,6>

The output is a list of lists of complex numbers.

<
   <1.000e+00-2.449e-16j>,
   <
      1.000e+00-2.449e-16j,
      -1.000e+00+1.225e-16j>,
   <
      1.000e+00-8.327e-16j,
      -5.000e-01+8.660e-01j,
      -5.000e-01-8.660e-01j>,
   <
      1.000e+00-8.882e-16j,
      2.220e-16+1.000e+00j,
      -1.000e+00+4.441e-16j,
      -6.661e-16-1.000e+00j>,
   <
      1.000e+00-5.551e-17j,
      3.090e-01+9.511e-01j,
      -8.090e-01+5.878e-01j,
      -8.090e-01-5.878e-01j,
      3.090e-01-9.511e-01j>,
   <
      1.000e+00-1.221e-15j,
      5.000e-01+8.660e-01j,
      -5.000e-01+8.660e-01j,
      -1.000e+00+6.106e-16j,
      -5.000e-01-8.660e-01j,
      5.000e-01-8.660e-01j>>

C

#include <stdio.h>
#include <math.h>

int main()
{
	double a, c, s, PI2 = atan2(1, 1) * 8;
	int n, i;

	for (n = 1; n < 10; n++) for (i = 0; i < n; i++) {
		c = s = 0;
		if (!i )		c =  1;
		else if(n == 4 * i)	s =  1;
		else if(n == 2 * i)	c = -1;
		else if(3 * n == 4 * i)	s = -1;
		else
			a = i * PI2 / n, c = cos(a), s = sin(a);

		if (c) printf("%.2g", c);
		printf(s == 1 ? "i" : s == -1 ? "-i" : s ? "%+.2gi" : "", s);
		printf(i == n - 1 ?"\n":",  ");
	}

	return 0;
}

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

class Program
{
    static IEnumerable<Complex> RootsOfUnity(int degree)
    {
        return Enumerable
            .Range(0, degree)
            .Select(element => Complex.FromPolarCoordinates(1, 2 * Math.PI * element / degree));
    }

    static void Main()
    {
        var degree = 3;
        foreach (var root in RootsOfUnity(degree))
        {
            Console.WriteLine(root);
        }
    }
}

Output:

(1, 0)
(-0,5, 0,866025403784439)
(-0,5, -0,866025403784438)

C++

#include <complex>
#include <cmath>
#include <iostream>

double const pi = 4 * std::atan(1);

int main()
{
  for (int n = 2; n <= 10; ++n)
  {
    std::cout << n << ": ";
    for (int k = 0; k < n; ++k)
      std::cout << std::polar(1, 2*pi*k/n) << " ";
    std::cout << std::endl;
  }
}

CoffeeScript

Most of the effort here is in formatting the results, and the output is still a bit clumsy.

# Find the n nth-roots of 1
nth_roots_of_unity = (n) ->
  (complex_unit_vector(2*Math.PI*i/n) for i in [1..n])

complex_unit_vector = (rad) ->
  new Complex(Math.cos(rad), Math.sin(rad))
  
class Complex
  constructor: (@real, @imag) ->
  toString: ->
    round_z = (n) ->
      if Math.abs(n) < 0.00005 then 0 else n
    fmt = (n) -> n.toFixed(3)
    real = round_z @real
    imag = round_z @imag
    s = ''
    if real and imag
      "#{fmt real}+#{fmt imag}i"
    else if real or !imag
      "#{fmt real}"
    else
      "#{fmt imag}i"
      
do ->
  for n in [2..5]
    console.log "---1 to the 1/#{n}"
    for root in nth_roots_of_unity n
      console.log root.toString()

output

> coffee nth_roots.coffee 
---1 to the 1/2
-1.000
1.000
---1 to the 1/3
-0.500+0.866i
-0.500+-0.866i
1.000
---1 to the 1/4
1.000i
-1.000
-1.000i
1.000
---1 to the 1/5
0.309+0.951i
-0.809+0.588i
-0.809+-0.588i
0.309+-0.951i
1.000

Common Lisp

(defun roots-of-unity (n)
 (loop for i below n
       collect (cis (* pi (/ (* 2 i) n)))))

The expression is slightly more complicated than necessary in order to preserve exact rational arithmetic until multiplying by pi. The author of this example is not a floating point expert and not sure whether this is actually useful; if not, the simpler expression is (cis (/ (* 2 pi i) n)).

Crystal

Translation of: Ruby
require "complex"

def roots_of_unity(n)
  (0...n).map { |k| Math.exp((2 * Math::PI * k / n).i) }
end
 
p roots_of_unity(3)

Or alternative

def roots_of_unity(n)
  (0...n).map { |k| Complex.new(Math.cos(2 * Math::PI * k / n), Math.sin(2 * Math::PI * k / n)) }
end
Output:
[(1+0.0i), (-0.4999999999999998+0.8660254037844387i), (-0.5000000000000004-0.8660254037844384i)]

D

Using std.complex:

import std.stdio, std.range, std.algorithm, std.complex;
import std.math: PI;

auto nthRoots(in int n) pure nothrow {
    return n.iota.map!(k => expi(PI * 2 * (k + 1) / n));
}

void main() {
    foreach (immutable i; 1 .. 6)
        writefln("#%d: [%(%5.2f, %)]", i, i.nthRoots);
}
Output:
#1: [ 1.00+ 0.00i]
#2: [-1.00+-0.00i,  1.00+ 0.00i]
#3: [-0.50+ 0.87i, -0.50+-0.87i,  1.00+ 0.00i]
#4: [-0.00+ 1.00i, -1.00+-0.00i,  0.00+-1.00i,  1.00+ 0.00i]
#5: [ 0.31+ 0.95i, -0.81+ 0.59i, -0.81+-0.59i,  0.31+-0.95i,  1.00+ 0.00i]

Delphi

Translation of: C#
program Roots_of_unity;

{$APPTYPE CONSOLE}

uses
  System.VarCmplx;

function RootOfUnity(degree: integer): Tarray<Variant>;
var
  k: Integer;
begin
  SetLength(result, degree);
  for k := 0 to degree - 1 do
    Result[k] := VarComplexFromPolar(1, 2 * pi * k / degree);
end;

const
  n = 3;
var
  num: Variant;
begin
  Writeln('Root of unity from ', n, ':'#10);
  for num in RootOfUnity(n) do
    Writeln(num);
  Readln;
end.
Output:
Root of unity from 3:

1 + 0i
-0,5 + 0,866025403784438i
-0,5 - 0,866025403784439i

EasyLang

Translation of: AWK
numfmt 4 0
for n = 2 to 5
   write n & ": "
   for root = 0 to n - 1
      real = cos (360 * root / n)
      imag = sin (360 * root / n)
      write real & " " & imag & "i"
      if root <> n - 1
         write ", "
      .
   .
   print ""
.
Output:
2: 1 0i, -1 0.0000i
3: 1 0i, -0.5000 0.8660i, -0.5000 -0.8660i
4: 1 0i, 0.0000 1i, -1 0.0000i, -0.0000 -1i
5: 1 0i, 0.3090 0.9511i, -0.8090 0.5878i, -0.8090 -0.5878i, 0.3090 -0.9511i

EchoLisp

(define (roots-1 n)
   (define theta (// (* 2 PI) n))
   (for/list ((i n))
      (polar 1. (* theta i))))

(roots-1 2)
     (1+0i -1+0i)
(roots-1 3)
     (1+0i -0.4999999999999998+0.8660254037844388i -0.5000000000000004-0.8660254037844384i)
(roots-1 4)
     (1+0i 0+i -1+0i 0-i)

ERRE

PROGRAM UNITY_ROOTS

!
! for rosettacode.org
!

BEGIN
   PRINT(CHR$(12);) !CLS
   N=5                                       ! this can be changed for any desired n
   ANGLE=0                                   ! start at ANGLE 0
   REPEAT
     REAL=COS(ANGLE)                         ! real axis is the x axis
     IF (ABS(REAL)<10^-5) THEN REAL=0 END IF ! get rid of annoying sci notation
     IMAG=SIN(ANGLE)                         ! imaginary axis is the y axis
     IF (ABS(IMAG)<10^-5) THEN IMAG=0 END IF ! get rid of annoying sci notation
     PRINT(REAL;"+";IMAG;"i")                ! answer on every line
     ANGLE+=(2*π)/N
                                             ! all the way around the circle at even intervals
   UNTIL ANGLE>=2*π
END PROGRAM

Note: Adapted from Qbasic version. π is the predefined constant Greek Pi.

Factor

USING: math.functions prettyprint ;

1 3 roots .
Output:
{
    1.0
    C{ -0.4999999999999998 0.8660254037844387 }
    C{ -0.5000000000000003 -0.8660254037844384 }
}

Forth

Complex numbers are not a native type in Forth, so we calculate the roots by hand.

: f0. ( f -- )
  fdup 0e 0.001e f~ if fdrop 0e then f. ;
: .roots ( n -- )
  dup 1 do
    pi i 2* 0 d>f f* dup 0 d>f f/          ( F: radians )
    fsincos cr ." real " f0. ." imag " f0.
  loop drop ;

3 set-precision
5 .roots

On the other hand, complex numbers are implemented by the FSL.

Works with: gforth version 0.7.9_20170308
Translation of: C++
require fsl-util.fs
require fsl/complex.fs

: abs= 1E-12 F~ ;
: clamp-to-0 FDUP 0E0 abs= IF FDROP 0E0 THEN ;
: zclamp-to-0
  clamp-to-0 FSWAP
  clamp-to-0 FSWAP ;
: .roots
  1+ 2 DO
    I . ." : "
    I 0 DO
      1E0 2E0 PI F* I S>F F* J S>F F/ polar> zclamp-to-0 z. SPACE
    LOOP
    CR
  LOOP ;
3 SET-PRECISION
5 .roots

Fortran

Sin/Cos + Scalar Loop

Works with: Fortran version ISO Fortran 90 and later
PROGRAM Roots

  COMPLEX :: root 
  INTEGER :: i, n
  REAL :: angle, pi

  pi = 4.0 * ATAN(1.0)
  DO n = 2, 7
    angle = 0.0
    WRITE(*,"(I1,A)", ADVANCE="NO") n,": "
    DO i = 1, n
      root = CMPLX(COS(angle), SIN(angle))
      WRITE(*,"(SP,2F7.4,A)", ADVANCE="NO") root, "j  "
      angle = angle + (2.0*pi / REAL(n))
    END DO
    WRITE(*,*)
  END DO

END PROGRAM Roots

Output

2: +1.0000+0.0000j  -1.0000+0.0000j   
3: +1.0000+0.0000j  -0.5000+0.8660j  -0.5000-0.8660j   
4: +1.0000+0.0000j  +0.0000+1.0000j  -1.0000+0.0000j  +0.0000-1.0000j   
5: +1.0000+0.0000j  +0.3090+0.9511j  -0.8090+0.5878j  -0.8090-0.5878j  +0.3090-0.9511j   
6: +1.0000+0.0000j  +0.5000+0.8660j  -0.5000+0.8660j  -1.0000+0.0000j  -0.5000-0.8660j  +0.5000-0.8660j 
7: +1.0000+0.0000j  +0.6235+0.7818j  -0.2225+0.9749j  -0.9010+0.4339j  -0.9010-0.4339j  -0.2225-0.9749j  +0.6235-0.7818j

Exp + Array-valued Statement

Works with: Fortran version ISO Fortran 90 and later
program unity
     real, parameter :: pi = 3.141592653589793
     complex, parameter :: i = (0, 1)
     complex, dimension(0:7-1) :: unit_circle
     integer :: n, j
     
     do n = 2, 7
          !!!! KEY STEP, does all the calculations in one statement !!!!
        unit_circle(0:n-1) = exp(2*i*pi/n * (/ (j, j=0, n-1) /) )

        write(*,"(i1,a)", advance="no") n, ": "
        write(*,"(sp,2f7.4,a)", advance="no") (unit_circle(j), "j  ", j = 0, n-1)
        write(*,*)
     end do
 end program unity

Frink

Calculates the angles in degrees, since Frink will use rational arithmetic (exact)

roots[n] :=
{
    a = makeArray[[n], 0]
    alpha = 360/n degrees
    theta = 0 degrees
    for k = 0 to length[a] - 1
    {
        a@k = cos[theta] + i sin[theta]
        theta = theta + alpha
    }
    a
}
Output:
setPrecision[8]

roots[3]
[1.0, ( -0.5 + 0.86602540498103642 i ), ( -0.5 - 0.86602540139124295 i )]

FunL

FunL has built-in support for complex numbers. i is predefined to represent the imaginary unit.

import math.{exp, Pi}

def rootsOfUnity( n ) = {exp( 2Pi i k/n ) | k <- 0:n}

println( rootsOfUnity(3) )
Output:
{1.0, -0.4999999999999998+0.8660254037844387i, -0.5000000000000004-0.8660254037844385i}

GAP

roots := n -> List([0 .. n-1], k -> E(n)^k);

r:=roots(7);
# [ 1, E(7), E(7)^2, E(7)^3, E(7)^4, E(7)^5, E(7)^6 ]

List(r, x -> x^7);
# [ 1, 1, 1, 1, 1, 1, 1 ]

Go

package main

import (
    "fmt"
    "math"
    "math/cmplx"
)

func main() {
    for n := 2; n <= 5; n++ {
        fmt.Printf("%d roots of 1:\n", n)
        for _, r := range roots(n) {
            fmt.Printf("  %18.15f\n", r)
        }
    }
}

func roots(n int) []complex128 {
    r := make([]complex128, n)
    for i := 0; i < n; i++ {
        r[i] = cmplx.Rect(1, 2*math.Pi*float64(i)/float64(n))
    }
    return r
}

Output:

2 roots of 1:
  ( 1.000000000000000+0.000000000000000i)
  (-1.000000000000000+0.000000000000000i)
3 roots of 1:
  ( 1.000000000000000+0.000000000000000i)
  (-0.500000000000000+0.866025403784439i)
  (-0.500000000000000-0.866025403784438i)
4 roots of 1:
  ( 1.000000000000000+0.000000000000000i)
  ( 0.000000000000000+1.000000000000000i)
  (-1.000000000000000+0.000000000000000i)
  (-0.000000000000000-1.000000000000000i)
5 roots of 1:
  ( 1.000000000000000+0.000000000000000i)
  ( 0.309016994374948+0.951056516295154i)
  (-0.809016994374947+0.587785252292473i)
  (-0.809016994374947-0.587785252292473i)
  ( 0.309016994374947-0.951056516295154i)

Groovy

Because the Groovy language does not provide a built-in facility for complex arithmetic, this example relies on the Complex class defined in the Complex numbers example.

/** The following closure creates a list of n evenly-spaced points around the unit circle,
  * useful in FFT calculations, among other things */
def rootsOfUnity = { n ->
    (0..<n).collect {
        Complex.fromPolar(1, 2 * Math.PI * it / n)
    }
}

Test program:

def tol = 0.000000001  // tolerance: acceptable "wrongness" to account for rounding error

((1..6) + [16]). each { n ->
    println "rootsOfUnity(${n}):"
    def rou = rootsOfUnity(n)
    rou.each { println it }
    assert rou[0] == 1
    def actual = n > 1 ? rou[Math.floor(n/2) as int] : rou[0]
    def expected = n > 1 ? (n%2 == 0) ? -1 : ~rou[Math.ceil(n/2) as int] : rou[0]
    def message = n > 1 ? (n%2 == 0) ? 'middle-most root should be -1' : 'two middle-most roots should be conjugates' : ''
    assert (actual - expected).abs() < tol : message
    assert rou.every { (it.rho - 1) < tol } : 'all roots should have magnitude 1'
    println()
}

Output:

rootsOfUnity(1):
1.0

rootsOfUnity(2):
1.0
-1.0 + 1.2246467991473532E-16i

rootsOfUnity(3):
1.0
-0.4999999998186198 + 0.8660254038891585i
-0.5000000003627604 - 0.8660254035749988i

rootsOfUnity(4):
1.0
6.123233995736766E-17 + i
-1.0 + 1.2246467991473532E-16i
-1.8369701987210297E-16 - i

rootsOfUnity(5):
1.0
0.30901699437494745 + 0.9510565162951535i
-0.8090169943749473 + 0.5877852522924732i
-0.8090169943749475 - 0.587785252292473i
0.30901699437494723 - 0.9510565162951536i

rootsOfUnity(6):
1.0
0.4999999998186201 + 0.8660254038891584i
-0.5000000003627598 + 0.8660254035749991i
-1.0 - 6.283181638240517E-10i
-0.4999999992744804 - 0.8660254042033175i
0.5000000009068993 - 0.8660254032608401i

rootsOfUnity(16):
1.0
0.9238795325112867 + 0.3826834323650898i
0.7071067811865476 + 0.7071067811865475i
0.38268343236508984 + 0.9238795325112867i
6.123233995736766E-17 + i
-0.3826834323650897 + 0.9238795325112867i
-0.7071067811865475 + 0.7071067811865476i
-0.9238795325112867 + 0.3826834323650899i
-1.0 + 1.2246467991473532E-16i
-0.9238795325112868 - 0.38268343236508967i
-0.7071067811865477 - 0.7071067811865475i
-0.38268343236509034 - 0.9238795325112865i
-1.8369701987210297E-16 - i
0.38268343236509 - 0.9238795325112866i
0.7071067811865474 - 0.7071067811865477i
0.9238795325112865 - 0.3826834323650904i

Haskell

import Data.Complex (Complex, cis)

rootsOfUnity :: (Enum a, Floating a) => a -> [Complex a]
rootsOfUnity n =
  [ cis (2 * pi * k / n)
  | k <- [0 .. n - 1] ]

main :: IO ()
main = mapM_ print $ rootsOfUnity 3
Output:
1.0 :+ 0.0
(-0.4999999999999998) :+ 0.8660254037844388
(-0.5000000000000004) :+ (-0.8660254037844384)

Icon and Unicon

procedure main()
   roots(10)
end

procedure roots(n)
   every n := 2 to 10 do
       every writes(n | (str_rep((0 to (n-1)) * 2 * &pi / n)) | "\n")
end

procedure str_rep(k)
  return " " || cos(k) || "+" || sin(k) || "i"
end

Notes:

IDL

For some example n:

n = 5
print,  exp( dcomplex( 0, 2*!dpi/n) ) ^ ( 1 + indgen(n) )

Outputs:

( 0.30901699, 0.95105652)( -0.80901699, 0.58778525)( -0.80901699, -0.58778525)( 0.30901699, -0.95105652)( 1.0000000, -1.1102230e-16)

J

   rou=: [: ^ 0j2p1 * i. % ]

   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
)

Java

Java doesn't have a nice way of dealing with complex numbers, so the real and imaginary parts are calculated separately based on the angle and printed together. There are also checks in this implementation to get rid of extremely small values (< 1.0E-3 where scientific notation sets in for Doubles). Instead, they are simply represented as 0. To remove those checks (for very high n's), remove both if statements.

import java.util.Locale;

public class Test {

    public static void main(String[] a) {
        for (int n = 2; n < 6; n++)
            unity(n);
    }

    public static void unity(int n) {
        System.out.printf("%n%d: ", n);

        //all the way around the circle at even intervals
        for (double angle = 0; angle < 2 * Math.PI; angle += (2 * Math.PI) / n) {

            double real = Math.cos(angle); //real axis is the x axis

            if (Math.abs(real) < 1.0E-3)
                real = 0.0; //get rid of annoying sci notation

            double imag = Math.sin(angle); //imaginary axis is the y axis

            if (Math.abs(imag) < 1.0E-3)
                imag = 0.0;

            System.out.printf(Locale.US, "(%9f,%9f) ", real, imag);
        }
    }
}
2: ( 1.000000, 0.000000) (-1.000000, 0.000000) 
3: ( 1.000000, 0.000000) (-0.500000, 0.866025) (-0.500000,-0.866025) 
4: ( 1.000000, 0.000000) ( 0.000000, 1.000000) (-1.000000, 0.000000) ( 0.000000,-1.000000) 
5: ( 1.000000, 0.000000) ( 0.309017, 0.951057) (-0.809017, 0.587785) (-0.809017,-0.587785) ( 0.309017,-0.951057)

JavaScript

function Root(angle) {
	with (Math) { this.r = cos(angle); this.i = sin(angle) }
}

Root.prototype.toFixed = function(p) {
	return this.r.toFixed(p) + (this.i >= 0 ? '+' : '') + this.i.toFixed(p) + 'i'
}

function roots(n) {
	var rs = [], teta = 2*Math.PI/n
	for (var angle=0, i=0; i<n; angle+=teta, i+=1) rs.push( new Root(angle) )
	return rs
}

for (var n=2; n<8; n+=1) {
	document.write(n, ': ')
	var rs=roots(n); for (var i=0; i<rs.length; i+=1) document.write( i ? ', ' : '', rs[i].toFixed(5) )
	document.write('<br>')
}
Output:
2: 1.00000+0.00000i, -1.00000+0.00000i
3: 1.00000+0.00000i, -0.50000+0.86603i, -0.50000-0.86603i
4: 1.00000+0.00000i, 0.00000+1.00000i, -1.00000+0.00000i, -0.00000-1.00000i
5: 1.00000+0.00000i, 0.30902+0.95106i, -0.80902+0.58779i, -0.80902-0.58779i, 0.30902-0.95106i
6: 1.00000+0.00000i, 0.50000+0.86603i, -0.50000+0.86603i, -1.00000+0.00000i, -0.50000-0.86603i, 0.50000-0.86603i
7: 1.00000+0.00000i, 0.62349+0.78183i, -0.22252+0.97493i, -0.90097+0.43388i, -0.90097-0.43388i, -0.22252-0.97493i, 0.62349-0.78183i

jq

Using the same example as in the Julia section, and representing x + i*y as [x,y]:

def nthroots(n):
  (8 * (1|atan)) as $twopi
  | range(0;n) | (($twopi * .) / n) as $angle | [ ($angle | cos), ($angle | sin) ];

nthroots(10)
$ uname -a
Darwin Mac-mini 13.3.0 Darwin Kernel Version 13.3.0: Tue Jun  3 21:27:35 PDT 2014; root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64

$ time jq -c -n -f Roots_of_unity.jq
[1,0]
[0.8090169943749475,0.5877852522924731]
[0.30901699437494745,0.9510565162951535]
[-0.30901699437494734,0.9510565162951536]
[-0.8090169943749473,0.5877852522924732]
[-1,1.2246467991473532e-16]
[-0.8090169943749475,-0.587785252292473]
[-0.30901699437494756,-0.9510565162951535]
[0.30901699437494723,-0.9510565162951536]
[0.8090169943749473,-0.5877852522924732]

real	0m0.015s
user	0m0.004s
sys	0m0.004s

Julia

nthroots(n::Integer) = [ cospi(2k/n)+sinpi(2k/n)im for k = 0:n-1 ]

(One could also use complex exponentials or other formulations.) For example, `nthroots(10)` gives:

10-element Array{Complex{Float64},1}:
            1.0+0.0im
  0.809017+0.587785im
  0.309017+0.951057im
 -0.309017+0.951057im
 -0.809017+0.587785im
           -1.0+0.0im
 -0.809017-0.587785im
 -0.309017-0.951057im
  0.309017-0.951057im
  0.809017-0.587785im

Kotlin

import java.lang.Math.*

data class Complex(val r: Double, val i: Double) {
    override fun toString() = when {
        i == 0.0 -> r.toString()
        r == 0.0 -> i.toString() + 'i'
        else -> "$r + ${i}i"
    }
}

fun unity_roots(n: Number) = (1..n.toInt() - 1).map {
    val a = it * 2 * PI / n.toDouble()
    var r = cos(a); if (abs(r) < 1e-6) r = 0.0
    var i = sin(a); if (abs(i) < 1e-6) i = 0.0
    Complex(r, i)
}

fun main(args: Array<String>) {
    (1..4).forEach { println(listOf(1) + unity_roots(it)) }
    println(listOf(1) + unity_roots(5.0))
}
Output:
[1]
[1, -1.0]
[1, -0.4999999999999998 + 0.8660254037844387i, -0.5000000000000004 + -0.8660254037844385i]
[1, 1.0i, -1.0, -1.0i]
[1, 0.30901699437494745 + 0.9510565162951535i, -0.8090169943749473 + 0.5877852522924732i, -0.8090169943749475 + -0.587785252292473i, 0.30901699437494723 + -0.9510565162951536i]

Lambdatalk

// cleandisp just to display 0 when n < 10^-10 
{def cleandisp
 {lambda {:n}
  {if {<= {abs :n} 1.e-10} then 0 else :n}}}
-> cleandisp

{def uroots
 {lambda {:n}
  {S.map {{lambda {:n :i}
                  {let { {:theta {/ {* 2 {PI} :i} :n}}  
                       } {cons {cleandisp {cos :theta}} 
                               {cleandisp {sin :theta}}}}} :n}
         {S.serie 0 {- :n 1}}} }}
-> uroots

{S.map {lambda {:i} {hr}i = :i -> {uroots :i}} {S.serie 2 10}}
-> i = 2 -> (1 0) (-1 0) i = 3 -> (1 0) (-0.4999999999999998 0.8660254037844388) (-0.5000000000000004 -0.8660254037844384) 
i = 4 -> (1 0) (0 1) (-1 0) (0 -1) 
i = 5 -> (1 0) (0.30901699437494745 0.9510565162951535) (-0.8090169943749473 0.5877852522924732) (-0.8090169943749475 -0.587785252292473) (0.30901699437494723 -0.9510565162951536) 
i = 6 -> (1 0) (0.5000000000000001 0.8660254037844386) (-0.4999999999999998 0.8660254037844388) (-1 0) (-0.5000000000000004 -0.8660254037844384) (0.5 -0.8660254037844386) 
i = 7 -> (1 0) (0.6234898018587336 0.7818314824680297) (-0.22252093395631434 0.9749279121818236) (-0.900968867902419 0.43388373911755823) (-0.9009688679024191 -0.433883739117558) (-0.2225209339563146 -0.9749279121818235) (0.6234898018587334 -0.7818314824680299) 
i = 8 -> (1 0) (0.7071067811865476 0.7071067811865475) (0 1) (-0.7071067811865475 0.7071067811865476) (-1 0) (-0.7071067811865477 -0.7071067811865475) (0 -1) (0.7071067811865475 -0.7071067811865477) 
i = 9 -> (1 0) (0.7660444431189781 0.6427876096865393) (0.17364817766693041 0.984807753012208) (-0.4999999999999998 0.8660254037844388) (-0.9396926207859083 0.3420201433256689) (-0.9396926207859084 -0.34202014332566866) (-0.5000000000000004 -0.8660254037844384) (0.17364817766692997 -0.9848077530122081) (0.7660444431189779 -0.6427876096865396) 
i = 10 -> (1 0) (0.8090169943749475 0.5877852522924731) (0.30901699437494745 0.9510565162951535) (-0.30901699437494734 0.9510565162951536) (-0.8090169943749473 0.5877852522924732) (-1 0) (-0.8090169943749475 -0.587785252292473) (-0.30901699437494756 -0.9510565162951535) (0.30901699437494723 -0.9510565162951536) (0.8090169943749473 -0.5877852522924732)

Lua

Complex numbers from the Lua implementation on the complex numbers page.

--defines addition, subtraction, negation, multiplication, division, conjugation, norms, and a conversion to strgs.
complex = setmetatable({
__add = function(u, v) return complex(u.real + v.real, u.imag + v.imag) end,
__sub = function(u, v) return complex(u.real - v.real, u.imag - v.imag) end,
__mul = function(u, v) return complex(u.real * v.real - u.imag * v.imag, u.real * v.imag + u.imag * v.real) end,
__div = function(u, v) return u * complex(v.real / v.norm, -v.imag / v.norm) end,
__unm = function(u) return complex(-u.real, -u.imag) end,
__concat = function(u, v)
    if type(u) == "table" then return u.real .. " + " .. u.imag .. "i" .. v
	elseif type(u) == "string" or type(u) == "number" then return u .. v.real .. " + " .. v.imag .. "i"
	end end,
__index = function(u, index)
  local operations = {
    norm = function(u) return u.real ^ 2 + u.imag ^ 2 end,
    conj = function(u) return complex(u.real, -u.imag) end,
  }
  return operations[index] and operations[index](u)
end,
__newindex = function() error() end
}, {
__call = function(z, realpart, imagpart) return setmetatable({real = realpart, imag = imagpart}, complex) end
} )
n = io.read() + 0
val = complex(math.cos(2*math.pi / n), math.sin(2*math.pi / n))
root = complex(1, 0)
for i = 1, n do
  root = root * val
  print(root .. "")
end

Maple

RootsOfUnity := proc( n )
    solve(z^n = 1, z);
end proc:
for i from 2 to 6 do
    printf( "%d: %a\n", i, [ RootsOfUnity(i) ] );
end do;

Output:

2: [1, -1]
3: [1, -1/2-1/2*I*3^(1/2), -1/2+1/2*I*3^(1/2)]
4: [1, -1, I, -I]
5: [1, 1/4*5^(1/2)-1/4+1/4*I*2^(1/2)*(5+5^(1/2))^(1/2), -1/4*5^(1/2)-1/4+1/4*I*2^(1/2)*(5-5^(1/2))^(1/2), -1/4*5^(1/2)-1/4-1/4*I*2^(1/2)*(5-5^(1/2))^(1/2), 1/4*5^(1/2)-1/4-1/4*I*2^(1/2)*(5+5^(1/2))^(1/2)]
6: [1, -1, 1/2*(-2-2*I*3^(1/2))^(1/2), -1/2*(-2-2*I*3^(1/2))^(1/2), 1/2*(-2+2*I*3^(1/2))^(1/2), -1/2*(-2+2*I*3^(1/2))^(1/2)]

Mathematica /Wolfram Language

Setting this up in Mathematica is easy, because it already handles complex numbers:

RootsUnity[nthroot_Integer?Positive] := Table[Exp[2 Pi I i/nthroot], {i, 0, nthroot - 1}]

Note that Mathematica will keep the expression as exact as possible. Simplifications can be made to more known (trigonometric) functions by using the function ExpToTrig. If only a numerical approximation is necessary the function N will transform the exact result to a numerical approximation. Examples (exact not simplified, exact simplified, approximated):

RootsUnity[2]
RootsUnity[3]
RootsUnity[4]
RootsUnity[5]

RootsUnity[2]//ExpToTrig
RootsUnity[3]//ExpToTrig
RootsUnity[4]//ExpToTrig
RootsUnity[5]//ExpToTrig

RootsUnity[2]//N
RootsUnity[3]//N
RootsUnity[4]//N
RootsUnity[5]//N

gives back:



MATLAB

function z = rootsOfUnity(n)

    assert(n >= 1,'n >= 1');
    z = roots([1 zeros(1,n-1) -1]);
    
end

Sample Output:

>> rootsOfUnity(3)

ans =

 -0.500000000000000 + 0.866025403784439i
 -0.500000000000000 - 0.866025403784439i
  1.000000000000000

Maxima

solve(1 = x^n, x)

Demonstration:

for n:1 thru 5 do display(solve(1 = x^n, x));

Output:

solve(1 = x, x) = [x = 1]
solve(1 = x^2, x) = [x = -1, x = 1]
solve(1 = x^3, x) = [x = (sqrt(3)*%i-1)/2, x = -(sqrt(3)*%i+1)/2, x = 1]
solve(1 = x^4, x) = [x = %i, x = -1, x = -%i, x = 1]
solve(1 = x^5, x) = [x = %e^((2*%i*%pi)/5), x = %e^((4*%i*%pi)/5), x = %e^(-(4*%i*%pi)/5), x = %e^(-(2*%i*%pi)/5), x = 1]

MiniScript

complexRoots = function(n)
    result = []
    for i in range(0, n-1)
        real = cos(2*pi * i/n)
        if abs(real) < 1e-6 then real = 0
        imag = sin(2*pi * i/n)
        if abs(imag) < 1e-6 then imag = 0
        result.push real + " " + "+" * (imag>=0) + imag + "i"
    end for
    return result
end function

for i in range(2,5)
    print i + ": " + complexRoots(i).join(", ")
end for
Output:
2: 1 +0i, -1 +0i
3: 1 +0i, -0.5 +0.866025i, -0.5 -0.866025i
4: 1 +0i, 0 +1i, -1 +0i, 0 -1i
5: 1 +0i, 0.309017 +0.951057i, -0.809017 +0.587785i, -0.809017 -0.587785i, 0.309017 -0.951057i

МК-61/52

П0	0	П1	ИП1	sin	ИП1	cos	С/П	2	пи
*	ИП0	/	ИП1	+	П1	БП	03

Nim

import complex, math, sequtils, strformat, strutils

proc roots(n: Positive): seq[Complex64] =
  for k in 0..<n:
    result.add rect(1.0, 2 * k.float * Pi / n.float)

proc toString(z: Complex64): string =
  &"{z.re:.3f} + {z.im:.3f}i"

for nr in 2..10:
  let result = roots(nr).map(toString).join(", ")
  echo &"{nr:2}: {result}"
Output:
 2: 1.000 + 0.000i, -1.000 + 0.000i
 3: 1.000 + 0.000i, -0.500 + 0.866i, -0.500 + -0.866i
 4: 1.000 + 0.000i, 0.000 + 1.000i, -1.000 + 0.000i, -0.000 + -1.000i
 5: 1.000 + 0.000i, 0.309 + 0.951i, -0.809 + 0.588i, -0.809 + -0.588i, 0.309 + -0.951i
 6: 1.000 + 0.000i, 0.500 + 0.866i, -0.500 + 0.866i, -1.000 + 0.000i, -0.500 + -0.866i, 0.500 + -0.866i
 7: 1.000 + 0.000i, 0.623 + 0.782i, -0.223 + 0.975i, -0.901 + 0.434i, -0.901 + -0.434i, -0.223 + -0.975i, 0.623 + -0.782i
 8: 1.000 + 0.000i, 0.707 + 0.707i, 0.000 + 1.000i, -0.707 + 0.707i, -1.000 + 0.000i, -0.707 + -0.707i, -0.000 + -1.000i, 0.707 + -0.707i
 9: 1.000 + 0.000i, 0.766 + 0.643i, 0.174 + 0.985i, -0.500 + 0.866i, -0.940 + 0.342i, -0.940 + -0.342i, -0.500 + -0.866i, 0.174 + -0.985i, 0.766 + -0.643i
10: 1.000 + 0.000i, 0.809 + 0.588i, 0.309 + 0.951i, -0.309 + 0.951i, -0.809 + 0.588i, -1.000 + 0.000i, -0.809 + -0.588i, -0.309 + -0.951i, 0.309 + -0.951i, 0.809 + -0.588i

OCaml

open Complex

let pi = 4. *. atan 1.

let () =
  for n = 1 to 10 do
    Printf.printf "%2d " n;
    for k = 1 to n do
      let ret = polar 1. (2. *. pi *. float_of_int k /. float_of_int n) in
        Printf.printf "(%f + %f i)" ret.re ret.im
    done;
    print_newline ()
  done

Octave

for j = 2 : 10
  printf("*** %d\n", j);
  for n = 1 : j
    disp(exp(2i*pi*n/j));
  endfor
  disp("");
endfor

OoRexx

Translation of: REXX
/*REXX program computes the  K  roots of unity  (which include complex roots).*/
parse Version v
Say v
parse arg n frac .                     /*get optional arguments from the C.L. */
if n==''    then n=1                   /*Not specified?  Then use the default.*/
if frac=''  then frac=5                /* "      "         "   "   "     "    */
start=abs(n)                           /*assume only one  K  is wanted.       */
if n<0      then start=1               /*Negative?  Then use a range of  K's. */
                                       /*display unity roots for a range,  or */
  do k=start  to abs(n)                /*                   just for one  K.  */
  say right(k 'roots of unity',40,"-") /*display a pretty separator with title*/
     do angle=0  by 360/k  for k       /*compute the angle for each root.     */
     rp=adjust(rxCalcCos(angle,,'D'))  /*compute real part via  COS  function.*/
     if left(rp,1)\=='-' then rp=" "rp /*not negative?  Then pad with a blank.*/
     ip=adjust(rxCalcSin(angle,,'D'))  /*compute imaginary part via SIN funct.*/
     if left(ip,1)\=='-' then ip="+"ip /*Not negative?  Then pad with  + char.*/
     if ip=0  then say rp              /*Only real part? Ignore imaginary part*/
              else say left(rp,frac+4)ip'i'   /*show the real & imaginary part*/
     end  /*angle*/
  end      /*k*/
exit                                   /*stick a fork in it,  we're all done. */
/*----------------------------------------------------------------------------*/
adjust: parse arg x; near0='1e-' || (digits()-digits()%10)   /*compute small #*/
        if abs(x)<near0  then x=0            /*if near zero, then assume zero.*/
        return format(x,,frac)/1             /*fraction digits past dec point.*/
::requires rxMath library
Output:
D:\>rexx nrootoo 5
REXX-ooRexx_4.2.0(MT)_64-bit 6.04 22 Feb 2014
------------------------5 roots of unity
 1
 0.30902 +0.95106i
-0.80902 +0.58779i
-0.80902 -0.58779i
 0.30902 -0.95106i

PARI/GP

vector(n,k,exp(2*Pi*I*k/n))

sqrtn() can give the first n'th root, from which the others by multiplying or powering.

nth_roots(n) = my(z);sqrtn(1,n,&z); vector(n,i, z^i);

Both the above give floating point complex numbers even when a root could be exact, like -1 or fourth root I.

quadgen() can be used for an exact 6th root. (Quads cannot be mixed with ordinary complex numbers, and they always print as w.)

sixth_root = quadgen(-3);   /* 6th root of unity, exact */
vector(6,n, sixth_root^n)   /* all the 6'th roots */

Pascal

Translation of: Fortran
Program Roots;

var
  root: record  // poor man's complex type.
    r: real;
    i: real;
  end;
  i, n:  integer;
  angle: real;

begin
  for n := 2 to 7 do
  begin
    angle := 0.0;
    write(n, ': ');
    for i := 1 to n do
    begin
      root.r := cos(angle);
      root.i := sin(angle);
      write(root.r:8:5, root.i:8:5, 'i ');
      angle := angle + (2.0 * pi / n);
    end;
    writeln;
  end;
end.

Output:

2:  1.00000 0.00000i -1.00000 0.00000i 
3:  1.00000 0.00000i -0.50000 0.86603i -0.50000-0.86603i 
4:  1.00000 0.00000i  0.00000 1.00000i -1.00000 0.00000i -0.00000-1.00000i 
5:  1.00000 0.00000i  0.30902 0.95106i -0.80902 0.58779i -0.80902-0.58779i  0.30902-0.95106i 
6:  1.00000 0.00000i  0.50000 0.86603i -0.50000 0.86603i -1.00000-0.00000i -0.50000-0.86603i  0.50000-0.86603i 
7:  1.00000 0.00000i  0.62349 0.78183i -0.22252 0.97493i -0.90097 0.43388i -0.90097-0.43388i -0.22252-0.97493i  0.62349-0.78183i 

Perl

Works with: Perl version 5.6.0

The root() function returns a list of the N many N'th roots of any complex Z, in this case 1.

use Math::Complex;
 
foreach my $n (2 .. 10) {
  printf "%2d", $n;
  my @roots = root(1,$n);
  foreach my $root (@roots) {
    $root->display_format(style => 'cartesian', format => '%.3f');
    print " $root";
  }
  print "\n";
}

Output:

 2 1.000 -1.000+0.000i
 3 1.000 -0.500+0.866i -0.500-0.866i
 4 1.000 0.000+1.000i -1.000+0.000i -0.000-1.000i
 5 1.000 0.309+0.951i -0.809+0.588i -0.809-0.588i 0.309-0.951i
 6 1.000 0.500+0.866i -0.500+0.866i -1.000+0.000i -0.500-0.866i 0.500-0.866i
 7 1.000 0.623+0.782i -0.223+0.975i -0.901+0.434i -0.901-0.434i -0.223-0.975i 0.623-0.782i
 8 1.000 0.707+0.707i 0.000+1.000i -0.707+0.707i -1.000+0.000i -0.707-0.707i -0.000-1.000i 0.707-0.707i
 9 1.000 0.766+0.643i 0.174+0.985i -0.500+0.866i -0.940+0.342i -0.940-0.342i -0.500-0.866i 0.174-0.985i 0.766-0.643i
10 1.000 0.809+0.588i 0.309+0.951i -0.309+0.951i -0.809+0.588i -1.000+0.000i -0.809-0.588i -0.309-0.951i 0.309-0.951i 0.809-0.588i

Phix

Translation of: AWK
with javascript_semantics
for n=2 to 10 do
    printf(1,"%2d:",n)
    for root=0 to n-1 do
        atom real = cos(2*PI*root/n)
        atom imag = sin(2*PI*root/n)
        printf(1,"%s %6.3f %6.3fi",{iff(root?",":""),real,imag})
    end for
    printf(1,"\n")
end for
Output:
 2:  1.000  0.000i, -1.000  0.000i
 3:  1.000  0.000i, -0.500  0.866i, -0.500 -0.866i
 4:  1.000  0.000i,  0.000  1.000i, -1.000  0.000i, -0.000 -1.000i
 5:  1.000  0.000i,  0.309  0.951i, -0.809  0.588i, -0.809 -0.588i,  0.309 -0.951i
 6:  1.000  0.000i,  0.500  0.866i, -0.500  0.866i, -1.000  0.000i, -0.500 -0.866i,  0.500 -0.866i
 7:  1.000  0.000i,  0.623  0.782i, -0.223  0.975i, -0.901  0.434i, -0.901 -0.434i, -0.223 -0.975i,  0.623 -0.782i
 8:  1.000  0.000i,  0.707  0.707i,  0.000  1.000i, -0.707  0.707i, -1.000  0.000i, -0.707 -0.707i, -0.000 -1.000i,  0.707 -0.707i
 9:  1.000  0.000i,  0.766  0.643i,  0.174  0.985i, -0.500  0.866i, -0.940  0.342i, -0.940 -0.342i, -0.500 -0.866i,  0.174 -0.985i,  0.766 -0.643i
10:  1.000  0.000i,  0.809  0.588i,  0.309  0.951i, -0.309  0.951i, -0.809  0.588i, -1.000  0.000i, -0.809 -0.588i, -0.309 -0.951i,  0.309 -0.951i,  0.809 -0.588i

PicoLisp

Translation of: C
(load "@lib/math.l")

(for N (range 2 10)
   (let Angle 0.0
      (prin N ": ")
      (for I N
         (let Ipart (sin Angle)
            (prin
               (round (cos Angle) 4)
               (if (lt0 Ipart) "-" "+")
               "j"
               (round (abs Ipart) 4)
               "  " ) )
         (inc 'Angle (*/ 2 pi N)) )
      (prinl) ) )

PL/I

complex_roots:
   procedure (N);
   declare N fixed binary nonassignable;
   declare x float, c fixed decimal (10,8) complex;
   declare twopi float initial ((4*asin(1.0)));

   do x = 0 to twopi by twopi/N;
      c = complex(cos(x), sin(x));
      put skip list (c);
   end;
end complex_roots;

   1.00000000+0.00000000I   
   0.80901700+0.58778524I   
   0.30901697+0.95105654I   
  -0.30901703+0.95105648I   
  -0.80901706+0.58778518I   
  -1.00000000-0.00000008I   
  -0.80901694-0.58778536I   
  -0.30901709-0.95105648I   
   0.30901712-0.95105648I   
   0.80901724-0.58778494I

Prolog

Solves the roots of unity symbolically, as complex powers of e.

roots(N, Rs) :-
    succ(Pn, N), numlist(0, Pn, Ks),
    maplist(root(N), Ks, Rs).

root(N, M, R2) :-
    R0 is (2*M) rdiv N,  % multiple of PI
    (R0 > 1 -> R1 is R0 - 2; R1 = R0),  % adjust for principal values
    cis(R1, R2).

cis(0, 1) :- !.
cis(1, -1) :- !.
cis(1 rdiv 2, i) :- !.
cis(-1 rdiv 2, -i) :- !.
cis(-1 rdiv Q, exp(-i*pi/Q)) :- !.
cis(1 rdiv Q, exp(i*pi/Q)) :- !.
cis(P rdiv Q, exp(P*i*pi/Q)).
Output:
?- roots(2,X).
X = [1, -1].

?- roots(3,X).
X = [1, exp(2*i*pi/3), exp(-2*i*pi/3)].

?- roots(4,X).
X = [1, i, -1, -i].

?- roots(5,X).
X = [1, exp(2*i*pi/5), exp(4*i*pi/5), exp(-4*i*pi/5), exp(-2*i*pi/5)].

?- roots(8,X), forall(member(A,X), format("~w~n", A)).
1
exp(i*pi/4)
i
exp(3*i*pi/4)
-1
exp(-3*i*pi/4)
-i
exp(-i*pi/4)
X = [1, exp(i*pi/4), i, exp(3*i*pi/4), -1, exp(... * ... * pi/4), -i, exp(... / ...)].

Python

Works with: Python version 3.7
import cmath


class Complex(complex):
    def __repr__(self):
        rp = '%7.5f' % self.real if not self.pureImag() else ''
        ip = '%7.5fj' % self.imag if not self.pureReal() else ''
        conj = '' if (
            self.pureImag() or self.pureReal() or self.imag < 0.0
        ) else '+'
        return '0.0' if (
            self.pureImag() and self.pureReal()
        ) else rp + conj + ip

    def pureImag(self):
        return abs(self.real) < 0.000005

    def pureReal(self):
        return abs(self.imag) < 0.000005


def croots(n):
    if n <= 0:
        return None
    return (Complex(cmath.rect(1, 2 * k * cmath.pi / n)) for k in range(n))
    # in pre-Python 2.6:
    #   return (Complex(cmath.exp(2j*k*cmath.pi/n)) for k in range(n))


for nr in range(2, 11):
    print(nr, list(croots(nr)))
Output:
2 [1.00000, -1.00000]
3 [1.00000, -0.50000+0.86603j, -0.50000-0.86603j]
4 [1.00000, 1.00000j, -1.00000, -1.00000j]
5 [1.00000, 0.30902+0.95106j, -0.80902+0.58779j, -0.80902-0.58779j, 0.30902-0.95106j]
6 [1.00000, 0.50000+0.86603j, -0.50000+0.86603j, -1.00000, -0.50000-0.86603j, 0.50000-0.86603j]
7 [1.00000, 0.62349+0.78183j, -0.22252+0.97493j, -0.90097+0.43388j, -0.90097-0.43388j, -0.22252-0.97493j, 0.62349-0.78183j]
8 [1.00000, 0.70711+0.70711j, 1.00000j, -0.70711+0.70711j, -1.00000, -0.70711-0.70711j, -1.00000j, 0.70711-0.70711j]
9 [1.00000, 0.76604+0.64279j, 0.17365+0.98481j, -0.50000+0.86603j, -0.93969+0.34202j, -0.93969-0.34202j, -0.50000-0.86603j, 0.17365-0.98481j, 0.76604-0.64279j]
10 [1.00000, 0.80902+0.58779j, 0.30902+0.95106j, -0.30902+0.95106j, -0.80902+0.58779j, -1.00000, -0.80902-0.58779j, -0.30902-0.95106j, 0.30902-0.95106j, 0.80902-0.58779j]

R

for(j in 2:10) {
  r <- sprintf("%d: ", j)
  for(n in 1:j) {
    r <- paste(r, format(exp(2i*pi*n/j), digits=4), ifelse(n<j, ",", ""))
  }
  print(r)
}

Output:

[1] "2:  -1+0i , 1-0i "
[1] "3:  -0.5+0.866i , -0.5-0.866i , 1-0i "
[1] "4:  0+1i , -1+0i , 0-1i , 1-0i "
[1] "5:  0.309+0.9511i , -0.809+0.5878i , -0.809-0.5878i , 0.309-0.9511i , 1-0i "
[1] "6:  0.5+0.866i , -0.5+0.866i , -1+0i , -0.5-0.866i , 0.5-0.866i , 1-0i "
[1] "7:  0.6235+0.7818i , -0.2225+0.9749i , -0.901+0.4339i , -0.901-0.4339i , -0.2225-0.9749i , 0.6235-0.7818i , 1-0i "
[1] "8:  0.7071+0.7071i , 0+1i , -0.7071+0.7071i , -1+0i , -0.7071-0.7071i , 0-1i , 0.7071-0.7071i , 1-0i "
[1] "9:  0.766+0.6428i , 0.1736+0.9848i , -0.5+0.866i , -0.9397+0.342i , -0.9397-0.342i , -0.5-0.866i , 0.1736-0.9848i , 0.766-0.6428i , 1-0i "
[1] "10:  0.809+0.5878i , 0.309+0.9511i , -0.309+0.9511i , -0.809+0.5878i , -1+0i , -0.809-0.5878i , -0.309-0.9511i , 0.309-0.9511i , 0.809-0.5878i , 1-0i "

Racket

#lang racket

(define (roots-of-unity n)
  (for/list ([k n])
    (make-polar 1 (* k (/ (* 2 pi) n)))))

Will produce a list of roots, for example:

> (for ([r (roots-of-unity 3)]) (displayln r))
1
-0.4999999999999998+0.8660254037844388i
-0.5000000000000004-0.8660254037844384i

Raku

(formerly Perl 6) Raku has a built-in function cis which returns a unitary complex number given its phase. Raku also defines the tau = 2*pi constant. Thus the k-th n-root of unity can simply be written cis(k*τ/n).

constant n = 10;
for ^n -> \k {
    say cis(k*τ/n);
}
Output:
1+0i
0.809016994374947+0.587785252292473i
0.309016994374947+0.951056516295154i
-0.309016994374947+0.951056516295154i
-0.809016994374947+0.587785252292473i
-1+1.22464679914735e-16i
-0.809016994374948-0.587785252292473i
-0.309016994374948-0.951056516295154i
0.309016994374947-0.951056516295154i
0.809016994374947-0.587785252292473i

Alternately, you could use the built-in .roots method to find the nth roots of any number.

.say for 1.roots(9)
Output:
1+0i
0.766044443118978+0.6427876096865393i
0.17364817766693041+0.984807753012208i
-0.4999999999999998+0.8660254037844387i
-0.9396926207859083+0.3420201433256689i
-0.9396926207859084-0.34202014332566866i
-0.5000000000000004-0.8660254037844384i
0.17364817766692997-0.9848077530122081i
0.7660444431189778-0.6427876096865396i

REXX

REXX doesn't have complex arithmetic, so the (real) values of   cos   and   sin   of multiples of   2 pi   radians (divided by K) are used.

Also, REXX doesn't have the   pi   constant defined, nor a   sin   or   cos   function, so they are included below within the REXX program.

Note:   this REXX version only   displays   5   significant digits past the decimal point,   but this can be overridden by specifying the 2nd argument when invoking the REXX program.   (See the value of the REXX variable   frac,   5th line).

/*REXX program computes the  K  roots of  unity  (which usually includes complex roots).*/
numeric digits length( pi() )  -  length(.)      /*use number of decimal digits in  pi. */
parse arg n frac .                               /*get optional arguments from the C.L. */
if   n=='' |    n==","  then     n= 1            /*Not specified?  Then use the default.*/
if frac='' | frac==","  then  frac= 5            /* "      "         "   "   "     "    */
                             start= abs(n)       /*assume only one  K  is wanted.       */
if n<0                  then start= 1            /*Negative?  Then use a range of  K's. */
      do #=start  to abs(n)                      /*show unity roots  (for a range or 1).*/
      say right(# 'roots of unity', 40, "─") ' (showing' frac "fractional decimal digits)"
         do angle=0  by pi*2/#  for #            /*compute the angle for each root.     */
         Rp= adj( cos(angle) )                   /*the    real   part via COS  function.*/
         Ip= adj( sin(angle) )                   /* "  imaginary   "   "  SIN      "    */
         if Rp>=0  then Rp= ' 'Rp                /*Not neg?  Then pad with a blank char.*/
         if Ip>=0  then Ip= '+'Ip                /* "   "      "   "    "  "  plus   "  */
         if Ip =0  then say Rp                   /*Only real part? Ignore imaginary part*/
                   else say left(Rp,frac+4)Ip'i' /*display the real and imaginary part. */
         end  /*angle*/
      end     /*#*/
exit 0                                           /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
adj: parse arg x; if abs(x) < ('1e-')(digits()*9%10)  then x= 0;  return format(x,,frac)/1
pi:  pi=3.141592653589793238462643383279502884197169399375105820974944592307816; return pi
r2r: pi2= pi() + pi;     return arg(1)  //  pi2  /*reduce #radians: -2pi ─► +2pi radians*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
cos: procedure; parse arg x;   x= r2r(x);   a= abs(x);   numeric fuzz min(9, digits() - 9)
     pi1_3=pi/3;      if a=pi1_3  then return .5;        if a=pi*.5 | a=pi2  then return 0
     if a=pi  then return -1;  if a=pi1_3*2  then return -.5;   z= 1;  _= 1;     $x= x * x
       do k=2  by 2  until p=z;  p=z;  _= -_ * $x / (k*(k-1));  z= z + _;  end;  return z
/*──────────────────────────────────────────────────────────────────────────────────────*/
sin: procedure; parse arg x;   x= r2r(x);                numeric fuzz min(5, digits() - 3)
     if abs(x)=pi  then return 0;              $x= x * x;       z= x;  _= x
       do k=2  by 2  until p=z;  p=z;  _= -_ * $x / (k*(k+1));  z= z + _;  end;   return z
output   when using the input of:     5
────────────────────────5 roots of unity  (showing 5 fractional decimal digits)
 1
 0.30902 +0.95106i
-0.80902 +0.58779i
-0.80902 -0.58779i
 0.30902 -0.95106i
output   when using the input of:     10   36
───────────────────────10 roots of unity  (showing 36 fractional decimal digits)
 1
 0.809016994374947424102293417182819059 +0.587785252292473129168705954639072769i
 0.309016994374947424102293417182819059 +0.951056516295153572116439333379382143i
-0.309016994374947424102293417182819059 +0.951056516295153572116439333379382143i
-0.809016994374947424102293417182819059 +0.587785252292473129168705954639072769i
-1
-0.809016994374947424102293417182819059 -0.587785252292473129168705954639072769i
-0.309016994374947424102293417182819059 -0.951056516295153572116439333379382143i
 0.309016994374947424102293417182819059 -0.951056516295153572116439333379382143i
 0.809016994374947424102293417182819059 -0.587785252292473129168705954639072769i
output   when using the input of:     -12

(Shown at five-sixths size.)

────────────────────────1 roots of unity  (showing 5 fractional decimal digits)
 1
────────────────────────2 roots of unity  (showing 5 fractional decimal digits)
 1
-1
────────────────────────3 roots of unity  (showing 5 fractional decimal digits)
 1
-0.5     +0.86603i
-0.5     -0.86603i
────────────────────────4 roots of unity  (showing 5 fractional decimal digits)
 1
 0       +1i
-1
 0       -1i
────────────────────────5 roots of unity  (showing 5 fractional decimal digits)
 1
 0.30902 +0.95106i
-0.80902 +0.58779i
-0.80902 -0.58779i
 0.30902 -0.95106i
────────────────────────6 roots of unity  (showing 5 fractional decimal digits)
 1
 0.5     +0.86603i
-0.5     +0.86603i
-1
-0.5     -0.86603i
 0.5     -0.86603i
────────────────────────7 roots of unity  (showing 5 fractional decimal digits)
 1
 0.62349 +0.78183i
-0.22252 +0.97493i
-0.90097 +0.43388i
-0.90097 -0.43388i
-0.22252 -0.97493i
 0.62349 -0.78183i
────────────────────────8 roots of unity  (showing 5 fractional decimal digits)
 1
 0.70711 +0.70711i
 0       +1i
-0.70711 +0.70711i
-1
-0.70711 -0.70711i
 0       -1i
 0.70711 -0.70711i
────────────────────────9 roots of unity  (showing 5 fractional decimal digits)
 1
 0.76604 +0.64279i
 0.17365 +0.98481i
-0.5     +0.86603i
-0.93969 +0.34202i
-0.93969 -0.34202i
-0.5     -0.86603i
 0.17365 -0.98481i
 0.76604 -0.64279i
───────────────────────10 roots of unity  (showing 5 fractional decimal digits)
 1
 0.80902 +0.58779i
 0.30902 +0.95106i
-0.30902 +0.95106i
-0.80902 +0.58779i
-1
-0.80902 -0.58779i
-0.30902 -0.95106i
 0.30902 -0.95106i
 0.80902 -0.58779i
───────────────────────11 roots of unity  (showing 5 fractional decimal digits)
 1
 0.84125 +0.54064i
 0.41542 +0.90963i
-0.14231 +0.98982i
-0.65486 +0.75575i
-0.95949 +0.28173i
-0.95949 -0.28173i
-0.65486 -0.75575i
-0.14231 -0.98982i
 0.41542 -0.90963i
 0.84125 -0.54064i
───────────────────────12 roots of unity  (showing 5 fractional decimal digits)
 1
 0.86603 +0.5i
 0.5     +0.86603i
 0       +1i
-0.5     +0.86603i
-0.86603 +0.5i
-1
-0.86603 -0.5i
-0.5     -0.86603i
 0       -1i
 0.5     -0.86603i
 0.86603 -0.5i

Ring

decimals(4)
for n = 2 to 5
    see string(n) + " : " 
    for root = 0 to n-1
        real = cos(2*3.14 * root / n)
        imag = sin(2*3.14 * root / n)
        see "" + real + " " + imag + "i"
        if root != n-1 see ", " ok
    next
    see nl
next

RLaB

RLaB can find the n-roots of unity by solving the polynomial equation

It uses the solver polyroots. Interested user is recommended to check the rlabplus manual for details on the solver and the parameters that tune the solver performance.

// specify polynomial
>> n = 10;
>> a = zeros(1,n+1); a[1] = 1; a[n+1] = -1;
>> polyroots(a)
   radius               roots           success
>> polyroots(a).roots
   -0.309016994 + 0.951056516i
   -0.809016994 + 0.587785252i
          -1 + 5.95570041e-23i
   -0.809016994 - 0.587785252i
   -0.309016994 - 0.951056516i
    0.309016994 - 0.951056516i
    0.809016994 - 0.587785252i
                        1 + 0i
    0.809016994 + 0.587785252i
    0.309016994 + 0.951056516i

RPL

≪ → r n
  ≪ { } 0 n 1 - FOR q 
       'r^INV(n)*e^(2*i*π*q/n)' →NUM + NEXT
≫ ≫ 'ROOTS' STO
1 3 ROOTS
Output:
1: { (1,0) (-0.5,0.866025403784) (-0.5,-0.866025403784) }

Ruby

def roots_of_unity(n)
  (0...n).map {|k| Complex.polar(1, 2 * Math::PI * k / n)}
end

p roots_of_unity(3)
Output:
 [(1+0.0i), (-0.4999999999999998+0.8660254037844387i), (-0.5000000000000004-0.8660254037844384i)]

Rust

Here we demonstrate initialization from polar complex coordinate, radius 1, e^πi/n, and raising the resulting complex number to the power 2k for k in 0..n-1, which generates approximate roots (see the Mathematica answer for a nice display of exact vs approximate). This code will require adding the num crate to one's rust project, typically in Cargo.toml [dependencies] \n num="0.2.0";

use num::Complex;
fn main() {
    let n = 8;
    let z = Complex::from_polar(&1.0,&(1.0*std::f64::consts::PI/n as f64));
    for k in 0..=n-1 {
        println!("e^{:2}πi/{} ≈ {:>14.3}",2*k,n,z.powf(2.0*k as f64));
    }
}
e^ 0πi/8 ≈   1.000+0.000i
e^ 2πi/8 ≈   0.707+0.707i
e^ 4πi/8 ≈   0.000+1.000i
e^ 6πi/8 ≈  -0.707+0.707i
e^ 8πi/8 ≈  -1.000+0.000i
e^10πi/8 ≈  -0.707-0.707i
e^12πi/8 ≈  -0.000-1.000i
e^14πi/8 ≈   0.707-0.707i

Scala

Using Complex class from task Arithmetic/Complex.

def rootsOfUnity(n:Int)=for(k <- 0 until n) yield Complex.fromPolar(1.0, 2*math.Pi*k/n)

Usage:

rootsOfUnity(3) foreach println

1.0+0.0i
-0.4999999999999998+0.8660254037844387i
-0.5000000000000004-0.8660254037844385i

Scheme

(define pi (* 4 (atan 1)))

(do ((n 2 (+ n 1)))
    ((> n 10))
    (display n)
    (do ((k 0 (+ k 1)))
        ((>= k n))
        (display " ")
        (display (make-polar 1 (* 2 pi (/ k n)))))
    (newline))

Seed7

$ include "seed7_05.s7i";
  include "float.s7i";
  include "complex.s7i";

const proc: main is func
  local
    var integer: n is 0;
    var integer: k is 0;
  begin
    for n range 2 to 10 do
      write(n lpad 2 <& ": ");
      for k range 0 to pred(n) do
        write(polar(1.0, 2.0 * PI * flt(k) / flt(n)) digits 4 lpad 15 <& " ");
      end for;
      writeln;
    end for;
  end func;

Output:

2:  1.0000+0.0000i -1.0000+0.0000i
 3:  1.0000+0.0000i -0.5000+0.8660i -0.5000-0.8660i
 4:  1.0000+0.0000i  0.0000+1.0000i -1.0000+0.0000i  0.0000-1.0000i
 5:  1.0000+0.0000i  0.3090+0.9511i -0.8090+0.5878i -0.8090-0.5878i  0.3090-0.9511i
 6:  1.0000+0.0000i  0.5000+0.8660i -0.5000+0.8660i -1.0000+0.0000i -0.5000-0.8660i  0.5000-0.8660i
 7:  1.0000+0.0000i  0.6235+0.7818i -0.2225+0.9749i -0.9010+0.4339i -0.9010-0.4339i -0.2225-0.9749i  0.6235-0.7818i
 8:  1.0000+0.0000i  0.7071+0.7071i  0.0000+1.0000i -0.7071+0.7071i -1.0000+0.0000i -0.7071-0.7071i  0.0000-1.0000i  0.7071-0.7071i
 9:  1.0000+0.0000i  0.7660+0.6428i  0.1736+0.9848i -0.5000+0.8660i -0.9397+0.3420i -0.9397-0.3420i -0.5000-0.8660i  0.1736-0.9848i  0.7660-0.6428i
10:  1.0000+0.0000i  0.8090+0.5878i  0.3090+0.9511i -0.3090+0.9511i -0.8090+0.5878i -1.0000+0.0000i -0.8090-0.5878i -0.3090-0.9511i  0.3090-0.9511i  0.8090-0.5878i

Sidef

Translation of: Raku
func roots_of_unity(n) {
    n.of { |j|
        exp(2i * Num.pi / n * j)
    }
}

roots_of_unity(5).each { |c|
    printf("%+.5f%+.5fi\n", c.reals)
}
Output:
+1.00000+0.00000i
+0.30902+0.95106i
-0.80902+0.58779i
-0.80902-0.58779i
+0.30902-0.95106i

Sparkling

function unity_roots(n) {
	// nth-root(1) = cos(2 * k * pi / n) + i * sin(2 * k * pi / n)
	return map(range(n), function(idx, k) {
		return {
			"re": cos(2 * k * M_PI / n),
			"im": sin(2 * k * M_PI / n)
		};
	});
}

// pirnt 6th roots of unity
foreach(unity_roots(6), function(k, v) {
	printf("%.3f%+.3fi\n", v.re, v.im);
});

Stata

n=7
exp(2i*pi()/n*(0::n-1))
                               1
    +-----------------------------+
  1 |                          1  |
  2 |   .623489802 + .781831482i  |
  3 |  -.222520934 + .974927912i  |
  4 |  -.900968868 + .433883739i  |
  5 |  -.900968868 - .433883739i  |
  6 |  -.222520934 - .974927912i  |
  7 |   .623489802 - .781831482i  |
    +-----------------------------+

Tcl

package require Tcl 8.5
namespace import tcl::mathfunc::*

set pi 3.14159265
for {set n 2} {$n <= 10} {incr n} {
    set angle 0.0
    set row $n:
    for {set i 1} {$i <= $n} {incr i} {
        lappend row [format %5.4f%+5.4fi [cos $angle] [sin $angle]]
        set angle [expr {$angle + 2*$pi/$n}]
    }
    puts $row
}

VBA

Public Sub roots_of_unity()
    For n = 2 To 9
        Debug.Print n; "th roots of 1:"
        For r00t = 0 To n - 1
            Debug.Print "   Root "; r00t & ": "; WorksheetFunction.Complex(Cos(2 * WorksheetFunction.Pi() * r00t / n), _
                Sin(2 * WorksheetFunction.Pi() * r00t / n))
        Next r00t
        Debug.Print
    Next n
End Sub
Output:
 2 th roots of 1:
   Root 0: 1
   Root 1: -1+1.22460635382238E-16i

 3 th roots of 1:
   Root 0: 1
   Root 1: -0.5+0.866025403784439i
   Root 2: -0.5-0.866025403784438i

 4 th roots of 1:
   Root 0: 1
   Root 1: 6.12303176911189E-17+i
   Root 2: -1+1.22460635382238E-16i
   Root 3: -1.83690953073357E-16-i

 5 th roots of 1:
   Root 0: 1
   Root 1: 0.309016994374947+0.951056516295154i
   Root 2: -0.809016994374947+0.587785252292473i
   Root 3: -0.809016994374948-0.587785252292473i
   Root 4: 0.309016994374947-0.951056516295154i

 6 th roots of 1:
   Root 0: 1
   Root 1: 0.5+0.866025403784439i
   Root 2: -0.5+0.866025403784439i
   Root 3: -1+1.22460635382238E-16i
   Root 4: -0.5-0.866025403784438i
   Root 5: 0.5-0.866025403784439i

 7 th roots of 1:
   Root 0: 1
   Root 1: 0.623489801858734+0.78183148246803i
   Root 2: -0.222520933956314+0.974927912181824i
   Root 3: -0.900968867902419+0.433883739117558i
   Root 4: -0.900968867902419-0.433883739117558i
   Root 5: -0.222520933956315-0.974927912181824i
   Root 6: 0.623489801858733-0.78183148246803i

 8 th roots of 1:
   Root 0: 1
   Root 1: 0.707106781186548+0.707106781186547i
   Root 2: 6.12303176911189E-17+i
   Root 3: -0.707106781186547+0.707106781186548i
   Root 4: -1+1.22460635382238E-16i
   Root 5: -0.707106781186548-0.707106781186547i
   Root 6: -1.83690953073357E-16-i
   Root 7: 0.707106781186547-0.707106781186548i

 9 th roots of 1:
   Root 0: 1
   Root 1: 0.766044443118978+0.642787609686539i
   Root 2: 0.17364817766693+0.984807753012208i
   Root 3: -0.5+0.866025403784439i
   Root 4: -0.939692620785908+0.342020143325669i
   Root 5: -0.939692620785908-0.342020143325669i
   Root 6: -0.5-0.866025403784438i
   Root 7: 0.17364817766693-0.984807753012208i
   Root 8: 0.766044443118978-0.64278760968654i

V (Vlang)

Translation of: Ring
import math

for n in 2..6 {
    print("${n}: ")
    for root in 0..n {
        real := math.cos(2 * 3.14 * root/n)
        imag := math.sin(2 * 3.14 * root/n)
        print("${real:.4} ${imag:.4}i")
        if root != n - 1 {print(", ")}
    }
    println("")
}

Wren

Translation of: Go
Library: Wren-complex
Library: Wren-fmt
import "./complex" for Complex
import "./fmt" for Fmt

var roots = Fn.new { |n|
    var r = List.filled(n, null)
    for (i in 0...n) r[i] = Complex.fromPolar(1, 2 * Num.pi * i / n)
    return r
}

for (n in 2..5) {
    Fmt.print("$d roots of 1:", n)
    for (r in roots.call(n)) Fmt.print("  $ 0.14z", r)
}
Output:
2 roots of 1:
   1.00000000000000 + 0.00000000000000i
  -1.00000000000000 + 0.00000000000000i
3 roots of 1:
   1.00000000000000 + 0.00000000000000i
  -0.50000000000000 + 0.86602540378444i
  -0.50000000000000 - 0.86602540378444i
4 roots of 1:
   1.00000000000000 + 0.00000000000000i
   0.00000000000000 + 1.00000000000000i
  -1.00000000000000 + 0.00000000000000i
  -0.00000000000000 - 1.00000000000000i
5 roots of 1:
   1.00000000000000 + 0.00000000000000i
   0.30901699437495 + 0.95105651629515i
  -0.80901699437495 + 0.58778525229247i
  -0.80901699437495 - 0.58778525229247i
   0.30901699437495 - 0.95105651629515i

zkl

Translation of: C
PI2:=(0.0).pi*2;
foreach n,i in ([1..9],n){
   c:=s:=0;
   if(not i)         c =  1;
   else if(n==4*i)   s =  1;
   else if(n==2*i)   c = -1;
   else if(3*n==4*i) s = -1;
   else a,c,s:=PI2*i/n,a.cos(),a.sin();
 
   if(c) print("%.2g".fmt(c));
   print( (s==1 and "i") or (s==-1 and "-i" or (s and "%+.2gi" or"")).fmt(s));
   print( (i==n-1) and "\n" or ",  ");
}
Output:
1
1,  -1
1,  -0.5+0.87i,  -0.5-0.87i
1,  i,  -1,  -i
1,  0.31+0.95i,  -0.81+0.59i,  -0.81-0.59i,  0.31-0.95i
1,  0.5+0.87i,  -0.5+0.87i,  -1,  -0.5-0.87i,  0.5-0.87i
1,  0.62+0.78i,  -0.22+0.97i,  -0.9+0.43i,  -0.9-0.43i,  -0.22-0.97i,  0.62-0.78i
1,  0.71+0.71i,  i,  -0.71+0.71i,  -1,  -0.71-0.71i,  -i,  0.71-0.71i
1,  0.77+0.64i,  0.17+0.98i,  -0.5+0.87i,  -0.94+0.34i,  -0.94-0.34i,  -0.5-0.87i,  0.17-0.98i,  0.77-0.64i