Roots of unity: Difference between revisions

m
m (→‎{{header|REXX}}: simplified some code, used a more idiomatic way for computing numeric digits (precision), added/changed whitespace and comments.)
 
(23 intermediate revisions by 17 users not shown)
Line 7:
Given &nbsp; <big>'''n'''</big>, &nbsp; find the &nbsp; <big>'''n'''<sup>th</sup></big> &nbsp; [[wp:Roots of unity|roots of unity]].
<br><br>
 
=={{header|11l}}==
<syntaxhighlight lang="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))</syntaxhighlight>
 
{{out}}
<pre>
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]
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight lang="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;
Line 42 ⟶ 65:
end loop;
end loop;
end Roots_Of_Unity;</langsyntaxhighlight>
[[Ada]] provides a direct implementation of polar composition of complex numbers ''x e''<sup>2&pi;''i y''</sup>. 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&pi;, N is used. Sample output:
<pre style="height:25ex;overflow:scroll">
Line 114 ⟶ 137:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}}
<langsyntaxhighlight lang="algol68">FORMAT complex fmt=$g(-6,4)"⊥"g(-6,4)$;
FOR root FROM 2 TO 10 DO
printf(($g(4)$,root));
Line 121 ⟶ 144:
OD;
printf($l$)
OD</langsyntaxhighlight>
Output:
<pre>
Line 133 ⟶ 156:
+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
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">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]</syntaxhighlight>
 
{{out}}
 
<pre> 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</pre>
 
=={{header|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.)
 
<syntaxhighlight lang="ats">
(*
 
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
 
(*------------------------------------------------------------------*)
</syntaxhighlight>
 
{{out}}
<pre>$ 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
 
</pre>
 
=={{header|AutoHotkey}}==
ahk forum: [http://www.autohotkey.com/forum/post-276712.html#276712 discussion]
<langsyntaxhighlight AutoHotkeylang="autohotkey">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</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ROOTS_OF_UNITY.AWK
BEGIN {
Line 159 ⟶ 603:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 169 ⟶ 613:
 
=={{header|BASIC}}==
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{trans|Java}}
For high n's, this may repeat the root of 1 + 0*i.
<langsyntaxhighlight lang="qbasic"> CLS
PI = 3.1415926#
n = 5 'this can be changed for any desired n
Line 184 ⟶ 629:
angle = angle + (2 * PI) / n
'all the way around the circle at even intervals
LOOP WHILE angle < 2 * PI</langsyntaxhighlight>
 
==={{header|BBC BASICBASIC256}}===
<syntaxhighlight lang="freebasic">twopi = 2 * pi
<lang bbcbasic> @% = &20408
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</syntaxhighlight>
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> @% = &20408
FOR n% = 2 TO 5
PRINT STR$(n%) ": " ;
Line 197 ⟶ 657:
NEXT
PRINT
NEXT n%</langsyntaxhighlight>
'''Output:'''
<pre>
Line 205 ⟶ 665:
5: 1.0000 0.0000i, 0.3090 0.9511i, -0.8090 0.5878i, -0.8090 -0.5878i, 0.3090 -0.9511i
</pre>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="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</syntaxhighlight>
{{out| Output}}<pre>
1, 0i
0.31, 0.95i
-0.81, 0.59i
-0.81, 0.59i
0.30, 0.95i
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="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</syntaxhighlight>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="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</syntaxhighlight>
Output:
<pre>
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
</pre>
 
==={{header|Liberty BASIC}}===
<syntaxhighlight lang="lb">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</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="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()</syntaxhighlight>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">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
</syntaxhighlight>
Output:
<pre>
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</pre>
 
==={{header|TI-89 BASIC}}===
<syntaxhighlight lang="ti89b">cZeros(x^n - 1, x)</syntaxhighlight>
For n=3 in exact mode, the results are
<syntaxhighlight lang="ti89b">{-1/2+√(3)/2*i, -1/2-√(3)/2*i, 1}</syntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|BASIC}}
<syntaxhighlight lang="qbasic">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</syntaxhighlight>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="freebasic">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</syntaxhighlight>
 
 
=={{header|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.
<syntaxhighlight lang="ursala">#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></syntaxhighlight>
The output is a list of lists of complex numbers.
<pre>
<
<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>></pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
 
Line 230 ⟶ 939:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 255 ⟶ 964:
}
}
}</langsyntaxhighlight>
Output:
<pre>(1, 0)
Line 262 ⟶ 971:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <complex>
#include <cmath>
#include <iostream>
Line 277 ⟶ 986:
std::cout << std::endl;
}
}</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
Most of the effort here is in formatting the results, and the output is still a bit clumsy.
<langsyntaxhighlight lang="coffeescript"># 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])
Line 308 ⟶ 1,017:
console.log "---1 to the 1/#{n}"
for root in nth_roots_of_unity n
console.log root.toString()</langsyntaxhighlight>
output
<pre>
Line 333 ⟶ 1,042:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun roots-of-unity (n)
(loop for i below n
collect (cis (* pi (/ (* 2 i) n)))))</langsyntaxhighlight>
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 <tt>(cis (/ (* 2 pi i) n))</tt>.
 
=={{header|Crystal}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">require "complex"
 
def roots_of_unity(n)
(0...n).map { |k| Math.exp((2 * Math::PI * k / n).i.exp) }
end
p roots_of_unity(3)
</syntaxhighlight>
</lang>
Or alternative
<langsyntaxhighlight lang="ruby">
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
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 361 ⟶ 1,070:
=={{header|D}}==
Using std.complex:
<langsyntaxhighlight lang="d">import std.stdio, std.range, std.algorithm, std.complex;
import std.math: PI;
 
Line 371 ⟶ 1,080:
foreach (immutable i; 1 .. 6)
writefln("#%d: [%(%5.2f, %)]", i, i.nthRoots);
}</langsyntaxhighlight>
{{out}}
<pre>#1: [ 1.00+ 0.00i]
Line 381 ⟶ 1,090:
{{libheader| System.VarCmplx}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Roots_of_unity;
 
Line 407 ⟶ 1,116:
Writeln(num);
Readln;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 416 ⟶ 1,125:
-0,5 - 0,866025403784439i
</pre>
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight>
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 ""
.
</syntaxhighlight>
 
{{out}}
<pre>
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
</pre>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(define (roots-1 n)
(define theta (// (* 2 PI) n))
Line 429 ⟶ 1,164:
(roots-1 4)
→ (1+0i 0+i -1+0i 0-i)
</syntaxhighlight>
</lang>
 
=={{header|ERRE}}==
<syntaxhighlight lang="text">
PROGRAM UNITY_ROOTS
 
Line 453 ⟶ 1,188:
UNTIL ANGLE>=2*π
END PROGRAM
</syntaxhighlight>
</lang>
Note: Adapted from Qbasic version. π is the predefined constant Greek Pi.
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: math.functions prettyprint ;
 
1 3 roots .</langsyntaxhighlight>
{{out}}
<pre>
Line 471 ⟶ 1,206:
=={{header|Forth}}==
Complex numbers are not a native type in Forth, so we calculate the roots by hand.
<langsyntaxhighlight lang="forth">: f0. ( f -- )
fdup 0e 0.001e f~ if fdrop 0e then f. ;
: .roots ( n -- )
Line 480 ⟶ 1,215:
 
3 set-precision
5 .roots</langsyntaxhighlight>
{{libheader|Forth Scientific Library}}
On the other hand, complex numbers are implemented by the FSL.
{{works with|gforth|0.7.9_20170308}}
{{trans|C++}}
<langsyntaxhighlight lang="forth">require fsl-util.fs
require fsl/complex.fs
 
Line 502 ⟶ 1,237:
LOOP ;
3 SET-PRECISION
5 .roots</langsyntaxhighlight>
 
=={{header|Fortran}}==
===Sin/Cos + Scalar Loop===
{{works with|Fortran|ISO Fortran 90 and later}}
<langsyntaxhighlight lang="fortran">PROGRAM Roots
 
COMPLEX :: root
Line 525 ⟶ 1,260:
END DO
 
END PROGRAM Roots</langsyntaxhighlight>
Output
2: +1.0000+0.0000j -1.0000+0.0000j
Line 536 ⟶ 1,271:
===Exp + Array-valued Statement===
{{works with|Fortran|ISO Fortran 90 and later}}
<langsyntaxhighlight lang="fortran">program unity
real, parameter :: pi = 3.141592653589793
complex, parameter :: i = (0, 1)
Line 550 ⟶ 1,285:
write(*,*)
end do
end program unity</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<lang 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</lang>
 
=={{header|Frink}}==
Calculates the angles in degrees, since Frink will use rational arithmetic (exact)
<syntaxhighlight lang="frink">
<lang Frink>
roots[n] :=
{
Line 585 ⟶ 1,302:
a
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 596 ⟶ 1,313:
=={{header|FunL}}==
FunL has built-in support for complex numbers. <code>i</code> is predefined to represent the imaginary unit.
<langsyntaxhighlight lang="funl">import math.{exp, Pi}
 
def rootsOfUnity( n ) = {exp( 2Pi i k/n ) | k <- 0:n}
 
println( rootsOfUnity(3) )</langsyntaxhighlight>
 
{{out}}
Line 606 ⟶ 1,323:
<pre>
{1.0, -0.4999999999999998+0.8660254037844387i, -0.5000000000000004-0.8660254037844385i}
</pre>
 
=={{header|FutureBasic}}==
<lang futurebasic>
include "ConsoleWindow"
 
dim as long n, root
dim as 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
</lang>
Output:
<pre>
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
</pre>
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">roots := n -> List([0 .. n-1], k -> E(n)^k);
 
r:=roots(7);
Line 643 ⟶ 1,332:
 
List(r, x -> x^7);
# [ 1, 1, 1, 1, 1, 1, 1 ]</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 669 ⟶ 1,358:
}
return r
}</langsyntaxhighlight>
Output:
<pre>2 roots of 1:
Line 692 ⟶ 1,381:
=={{header|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#Groovy|Complex numbers]] example.
<langsyntaxhighlight lang="groovy">/** 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 ->
Line 698 ⟶ 1,387:
Complex.fromPolar(1, 2 * Math.PI * it / n)
}
}</langsyntaxhighlight>
Test program:
<langsyntaxhighlight lang="groovy">def tol = 0.000000001 // tolerance: acceptable "wrongness" to account for rounding error
 
((1..6) + [16]). each { n ->
Line 713 ⟶ 1,402:
assert rou.every { (it.rho - 1) < tol } : 'all roots should have magnitude 1'
println()
}</langsyntaxhighlight>
Output:
<pre style="height:25ex;overflow:scroll;">rootsOfUnity(1):
Line 767 ⟶ 1,456:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Complex (Complex, cis)
 
rootsOfUnity :: (Enum a, Floating a) => a -> [Complex a]
Line 775 ⟶ 1,464:
 
main :: IO ()
main = mapM_ print $ rootsOfUnity 3</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight lang="haskell">1.0 :+ 0.0
(-0.4999999999999998) :+ 0.8660254037844388
(-0.5000000000000004) :+ (-0.8660254037844384)</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight lang="icon">procedure main()
roots(10)
end
Line 793 ⟶ 1,482:
procedure str_rep(k)
return " " || cos(k) || "+" || sin(k) || "i"
end</langsyntaxhighlight>
Notes:
* The [[:Category:Icon_Programming_Library|The Icon Programming Library]] implements a complex type but not a polar type
Line 799 ⟶ 1,488:
=={{header|IDL}}==
For some example <tt>n</tt>:
<langsyntaxhighlight lang="idl">n = 5
print, exp( dcomplex( 0, 2*!dpi/n) ) ^ ( 1 + indgen(n) )</langsyntaxhighlight>
Outputs:
<langsyntaxhighlight lang="idl">( 0.30901699, 0.95105652)( -0.80901699, 0.58778525)( -0.80901699, -0.58778525)( 0.30901699, -0.95105652)( 1.0000000, -1.1102230e-16)</langsyntaxhighlight>
 
=={{header|J}}==
<langsyntaxhighlight lang="j"> rou=: [: ^ 0j2p1 * i. % ]
 
rou 4
Line 811 ⟶ 1,500:
 
rou 5
1 0.309017j0.951057 _0.809017j0.587785 _0.809017j_0.587785 0.309017j_0.951057</langsyntaxhighlight>
The computation can also be written as a loop, shown here for comparison only.
<langsyntaxhighlight lang="j">rou1=: 3 : 0
z=. 0 $ r=. ^ o. 0j2 % y [ e=. 1
for. i.y do.
Line 820 ⟶ 1,509:
end.
z
)</langsyntaxhighlight>
 
=={{header|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 <tt>Double</tt>s). Instead, they are simply represented as 0. To remove those checks (for very high <tt>n</tt>'s), remove both if statements.
<langsyntaxhighlight lang="java">import java.util.Locale;
 
public class Test {
Line 852 ⟶ 1,541:
}
}
}</langsyntaxhighlight>
 
<pre>2: ( 1.000000, 0.000000) (-1.000000, 0.000000)
Line 860 ⟶ 1,549:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">function Root(angle) {
with (Math) { this.r = cos(angle); this.i = sin(angle) }
}
Line 879 ⟶ 1,568:
document.write('<br>')
}
</syntaxhighlight>
</lang>
{{Output}}
<pre>2: 1.00000+0.00000i, -1.00000+0.00000i
Line 890 ⟶ 1,579:
=={{header|jq}}==
Using the same example as in the Julia section, and representing x + i*y as [x,y]:
<langsyntaxhighlight lang="jq">def nthroots(n):
(8 * (1|atan)) as $twopi
| range(0;n) | (($twopi * .) / n) as $angle | [ ($angle | cos), ($angle | sin) ];
 
nthroots(10)</langsyntaxhighlight><syntaxhighlight lang ="jq">$ 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
 
Line 912 ⟶ 1,601:
user 0m0.004s
sys 0m0.004s
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">nthroots(n::Integer) = [ cospi(2k/n)+sinpi(2k/n)im for k = 0:n-1 ]</langsyntaxhighlight>
(One could also use complex exponentials or other formulations.) For example, `nthroots(10)` gives:
<pre>
Line 932 ⟶ 1,621:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">import java.lang.Math.*
 
data class Complex(val r: Double, val i: Double) {
Line 952 ⟶ 1,641:
(1..4).forEach { println(listOf(1) + unity_roots(it)) }
println(listOf(1) + unity_roots(5.0))
}</langsyntaxhighlight>
{{out}}
<pre>[1]
Line 961 ⟶ 1,650:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
// cleandisp just to display 0 when n < 10^-10
{def cleandisp
Line 987 ⟶ 1,676:
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)
 
</syntaxhighlight>
</lang>
 
=={{header|Liberty BASIC}}==
<lang lb>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</lang>
 
=={{header|Lua}}==
Complex numbers from the Lua implementation on the complex numbers page.
<langsyntaxhighlight lang="lua">--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,
Line 1,075 ⟶ 1,708:
root = root * val
print(root .. "")
end</langsyntaxhighlight>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">RootsOfUnity := proc( n )
solve(z^n = 1, z);
end proc:</langsyntaxhighlight>
<langsyntaxhighlight Maplelang="maple">for i from 2 to 6 do
printf( "%d: %a\n", i, [ RootsOfUnity(i) ] );
end do;</langsyntaxhighlight>
Output:
<langsyntaxhighlight Maplelang="maple">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)]</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Setting this up in Mathematica is easy, because it already handles complex numbers:
<langsyntaxhighlight Mathematicalang="mathematica">RootsUnity[nthroot_Integer?Positive] := Table[Exp[2 Pi I i/nthroot], {i, 0, nthroot - 1}]</langsyntaxhighlight>
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):
<pre>RootsUnity[2]
Line 1,141 ⟶ 1,774:
 
=={{header|MATLAB}}==
<langsyntaxhighlight MATLABlang="matlab">function z = rootsOfUnity(n)
 
assert(n >= 1,'n >= 1');
z = roots([1 zeros(1,n-1) -1]);
end</langsyntaxhighlight>
Sample Output:
<langsyntaxhighlight MATLABlang="matlab">>> rootsOfUnity(3)
 
ans =
Line 1,154 ⟶ 1,787:
-0.500000000000000 + 0.866025403784439i
-0.500000000000000 - 0.866025403784439i
1.000000000000000 </langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">solve(1 = x^n, x)</langsyntaxhighlight>
Demonstration:
<langsyntaxhighlight lang="maxima">for n:1 thru 5 do display(solve(1 = x^n, x));</langsyntaxhighlight>
Output:
<langsyntaxhighlight lang="maxima">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]</langsyntaxhighlight>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
<lang MiniScript>
complexRoots = function(n)
result = []
Line 1,183 ⟶ 1,816:
for i in range(2,5)
print i + ": " + complexRoots(i).join(", ")
end for</langsyntaxhighlight>
 
{{out}}
Line 1,192 ⟶ 1,825:
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">П0 0 П1 ИП1 sin ИП1 cos С/П 2 пи
* ИП0 / ИП1 + П1 БП 03</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Python}}
<lang nim>import complex, math
 
<syntaxhighlight lang="nim">import complex, math, sequtils, strformat, strutils
proc rect(r, phi: float): Complex = (r * cos(phi), sin(phi))
 
proc crootsroots(n: Positive): seq[ComplexComplex64] =
for k in 0..<n:
result = @[]
result.add rect(1.0, 2 * k.float * Pi / n.float)
if n <= 0: return
 
for k in 0 .. < n:
proc toString(z: Complex64): string =
result.add rect(1, 2 * k.float * Pi / n.float)
&"{z.re:.3f} + {z.im:.3f}i"
 
for nr in 2..10:
let result = roots(nr).map(toString).join(", ")
echo nr, " ", croots(nr)</lang>
echo &"{nr:2}: {result}"
Output:
</syntaxhighlight>
<pre>2 @[(1.0, 0.0), (-1.0, 1.224646799147353e-16)]
 
3 @[(1.0, 0.0), (-0.4999999999999998, 0.8660254037844387), (-0.5000000000000004, -0.8660254037844384)]
{{out}}
4 @[(1.0, 0.0), (6.123233995736766e-17, 1.0), (-1.0, 1.224646799147353e-16), (-1.83697019872103e-16, -1.0)]
<pre> 2: 1.000 + 0.000i, -1.000 + 0.000i
5 @[(1.0, 0.0), (0.3090169943749475, 0.9510565162951535), (-0.8090169943749473, 0.5877852522924732), (-0.8090169943749476, -0.587785252292473), (0.3090169943749472, -0.9510565162951536)]
3: 1.000 + 0.000i, -0.500 + 0.866i, -0.500 + -0.866i
6 @[(1.0, 0.0), (0.5000000000000001, 0.8660254037844386), (-0.4999999999999998, 0.8660254037844387), (-1.0, 1.224646799147353e-16), (-0.5000000000000004, -0.8660254037844384), (0.5000000000000001, -0.8660254037844386)]
4: 1.000 + 0.000i, 0.000 + 1.000i, -1.000 + 0.000i, -0.000 + -1.000i
7 @[(1.0, 0.0), (0.6234898018587336, 0.7818314824680298), (-0.2225209339563143, 0.9749279121818236), (-0.900968867902419, 0.4338837391175582), (-0.9009688679024191, -0.433883739117558), (-0.2225209339563146, -0.9749279121818236), (0.6234898018587334, -0.7818314824680299)]
5: 1.000 + 0.000i, 0.309 + 0.951i, -0.809 + 0.588i, -0.809 + -0.588i, 0.309 + -0.951i
8 @[(1.0, 0.0), (0.7071067811865476, 0.7071067811865475), (6.123233995736766e-17, 1.0), (-0.7071067811865475, 0.7071067811865476), (-1.0, 1.224646799147353e-16), (-0.7071067811865477, -0.7071067811865475), (-1.83697019872103e-16, -1.0), (0.7071067811865474, -0.7071067811865477)]
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
9 @[(1.0, 0.0), (0.766044443118978, 0.6427876096865393), (0.1736481776669304, 0.984807753012208), (-0.4999999999999998, 0.8660254037844387), (-0.9396926207859083, 0.3420201433256689), (-0.9396926207859084, -0.3420201433256687), (-0.5000000000000004, -0.8660254037844384), (0.17364817766693, -0.9848077530122081), (0.7660444431189778, -0.6427876096865396)]
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
10 @[(1.0, 0.0), (0.8090169943749475, 0.5877852522924731), (0.3090169943749475, 0.9510565162951535), (-0.3090169943749473, 0.9510565162951536), (-0.8090169943749473, 0.5877852522924732), (-1.0, 1.224646799147353e-16), (-0.8090169943749476, -0.587785252292473), (-0.3090169943749476, -0.9510565162951535), (0.3090169943749472, -0.9510565162951536), (0.8090169943749473, -0.5877852522924734)]</pre>
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</pre>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">open Complex
 
let pi = 4. *. atan 1.
Line 1,233 ⟶ 1,868:
done;
print_newline ()
done</langsyntaxhighlight>
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">for j = 2 : 10
printf("*** %d\n", j);
for n = 1 : j
Line 1,242 ⟶ 1,877:
endfor
disp("");
endfor</langsyntaxhighlight>
 
=={{header|OoRexx}}==
{{trans|REXX}}
<langsyntaxhighlight lang="oorexx">/*REXX program computes the K roots of unity (which include complex roots).*/
parse Version v
Say v
Line 1,271 ⟶ 1,906:
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</langsyntaxhighlight>
{{out}}
<pre>D:\>rexx nrootoo 5
Line 1,283 ⟶ 1,918:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">vector(n,k,exp(2*Pi*I*k/n))</langsyntaxhighlight>
 
<code>sqrtn()</code> can give the first n'th root, from which the others by multiplying or powering.
 
<langsyntaxhighlight lang="parigp">nth_roots(n) = my(z);sqrtn(1,n,&z); vector(n,i, z^i);</langsyntaxhighlight>
 
Both the above give floating point complex numbers even when a root could be exact, like <code>-1</code> or fourth root <code>I</code>.
Line 1,293 ⟶ 1,928:
<code>quadgen()</code> can be used for an exact 6th root. (Quads cannot be mixed with ordinary complex numbers, and they always print as <code>w</code>.)
 
<langsyntaxhighlight lang="parigp">sixth_root = quadgen(-3); /* 6th root of unity, exact */
vector(6,n, sixth_root^n) /* all the 6'th roots */</langsyntaxhighlight>
 
=={{header|Pascal}}==
{{trans|Fortran}}
<langsyntaxhighlight lang="pascal">Program Roots;
 
var
Line 1,322 ⟶ 1,957:
writeln;
end;
end.</langsyntaxhighlight>
Output:
<pre>
Line 1,339 ⟶ 1,974:
The <code>root()</code> function returns a list of the N many N'th roots of any complex Z, in this case 1.
 
<langsyntaxhighlight lang="perl">use Math::Complex;
foreach my $n (2 .. 10) {
Line 1,349 ⟶ 1,984:
}
print "\n";
}</langsyntaxhighlight>
Output:
<pre>
Line 1,365 ⟶ 2,000:
=={{header|Phix}}==
{{trans|AWK}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>for n=2 to 10 do
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
printf(1,"%2d:",n)
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
for root=0 to n-1 do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d:"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
atom real = cos(2*PI*root/n)
<span style="color: #008080;">for</span> <span style="color: #000000;">root</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
atom imag = sin(2*PI*root/n)
<span style="color: #004080;">atom</span> <span style="color: #000000;">real</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">*</span><span style="color: #000000;">root</span><span style="color: #0000FF;">/</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
printf(1,"%s %6.3f %6.3fi",{iff(root?",":""),real,imag})
<span style="color: #004080;">atom</span> <span style="color: #000000;">imag</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">*</span><span style="color: #000000;">root</span><span style="color: #0000FF;">/</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s %6.3f %6.3fi"</span><span style="color: #0000FF;">,{</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">root</span><span style="color: #0000FF;">?</span><span style="color: #008000;">","</span><span style="color: #0000FF;">:</span><span style="color: #008000;">""</span><span style="color: #0000FF;">),</span><span style="color: #000000;">real</span><span style="color: #0000FF;">,</span><span style="color: #000000;">imag</span><span style="color: #0000FF;">})</span>
printf(1,"\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre style="font-size: 10px">
2: 1.000 0.000i, -1.000 0.000i
Line 1,388 ⟶ 2,027:
=={{header|PicoLisp}}==
{{trans|C}}
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/math.l")
 
(for N (range 2 10)
Line 1,402 ⟶ 2,041:
" " ) )
(inc 'Angle (*/ 2 pi N)) )
(prinl) ) )</langsyntaxhighlight>
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">complex_roots:
procedure (N);
declare N fixed binary nonassignable;
Line 1,426 ⟶ 2,065:
-0.30901709-0.95105648I
0.30901712-0.95105648I
0.80901724-0.58778494I </langsyntaxhighlight>
 
=={{header|Prolog}}==
Solves the roots of unity symbolically, as complex powers of e.
<syntaxhighlight lang="prolog">
<lang Prolog>
roots(N, Rs) :-
succ(Pn, N), numlist(0, Pn, Ks),
Line 1,447 ⟶ 2,086:
cis(1 rdiv Q, exp(i*pi/Q)) :- !.
cis(P rdiv Q, exp(P*i*pi/Q)).
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,473 ⟶ 2,112:
X = [1, exp(i*pi/4), i, exp(3*i*pi/4), -1, exp(... * ... * pi/4), -i, exp(... / ...)].
</pre>
 
=={{header|PureBasic}}==
<lang 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()</lang>
 
=={{header|Python}}==
{{works with|Python|3.7}}
<langsyntaxhighlight lang="python">import cmath
 
 
Line 1,520 ⟶ 2,145:
 
for nr in range(2, 11):
print(nr, list(croots(nr)))</langsyntaxhighlight>
{{Out}}
<pre>2 [1.00000, -1.00000]
Line 1,533 ⟶ 2,158:
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">for(j in 2:10) {
r <- sprintf("%d: ", j)
for(n in 1:j) {
Line 1,539 ⟶ 2,164:
}
print(r)
}</langsyntaxhighlight>
Output:
<pre>
Line 1,554 ⟶ 2,179:
 
=={{header|Racket}}==
<langsyntaxhighlight Racketlang="racket">#lang racket
 
(define (roots-of-unity n)
(for/list ([k n])
(make-polar 1 (* k (/ (* 2 pi) n)))))</langsyntaxhighlight>
Will produce a list of roots, for example:
<pre>
Line 1,570 ⟶ 2,195:
Raku has a built-in function <tt>cis</tt> which returns a unitary complex number given its phase. Raku also defines the <tt>tau = 2*pi</tt> constant. Thus the k-th n-root of unity can simply be written <tt>cis(k*τ/n)</tt>.
 
<syntaxhighlight lang="raku" perl6line>constant n = 10;
for ^n -> \k {
say cis(k*τ/n);
}</langsyntaxhighlight>
 
{{out}}
Line 1,586 ⟶ 2,211:
0.309016994374947-0.951056516295154i
0.809016994374947-0.587785252292473i</pre>
 
Alternately, you could use the built-in .roots method to find the nth roots of any number.
 
<syntaxhighlight lang="raku" line>.say for 1.roots(9)</syntaxhighlight>
{{out}}
<pre>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</pre>
 
=={{header|REXX}}==
Line 1,593 ⟶ 2,232:
 
Note: &nbsp; this REXX version only &nbsp; ''displays'' &nbsp; '''5''' &nbsp; significant digits past the decimal point, &nbsp; but this can be overridden by specifying the 2<sup>nd</sup> argument when invoking the REXX program. &nbsp;
(See the value of the REXX variable &nbsp; '''frac''', &nbsp; 45<sup>th</sup> line).
<langsyntaxhighlight lang="rexx">/*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. */
Line 1,609 ⟶ 2,248:
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 /*#*/
Line 1,624 ⟶ 2,263:
/*──────────────────────────────────────────────────────────────────────────────────────*/
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; $x= x * x
do k=2 by 2 until p=z; p=z; _= -_ * $x / (k*(k+1)); z= z + _; end; return z</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 5 </tt>}}
<pre>
Line 1,747 ⟶ 2,386:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
decimals(4)
for n = 2 to 5
Line 1,759 ⟶ 2,398:
see nl
next
</syntaxhighlight>
</lang>
 
=={{header|RLaB}}==
Line 1,765 ⟶ 2,404:
:<math>x^n - 1 = 0.</math>
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.
<langsyntaxhighlight RLaBlang="rlab">// specify polynomial
>> n = 10;
>> a = zeros(1,n+1); a[1] = 1; a[n+1] = -1;
Line 1,780 ⟶ 2,419:
1 + 0i
0.809016994 + 0.587785252i
0.309016994 + 0.951056516i</langsyntaxhighlight>
 
=={{header|RPL}}==
≪ → r n
≪ { } 0 n 1 - '''FOR''' q
'r^INV(n)*e^(2*i*π*q/n)' →NUM + '''NEXT'''
≫ ≫ 'ROOTS' STO
 
1 3 ROOTS
{{out}}
<pre>
1: { (1,0) (-0.5,0.866025403784) (-0.5,-0.866025403784) }
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def roots_of_unity(n)
(0...n).map {|k| Complex.polar(1, 2 * Math::PI * k / n)}
end
 
p roots_of_unity(3)</langsyntaxhighlight>
 
{{out}}
Line 1,793 ⟶ 2,444:
[(1+0.0i), (-0.4999999999999998+0.8660254037844387i), (-0.5000000000000004-0.8660254037844384i)]
</pre>
 
=={{header|Run BASIC}}==
<lang runbasic>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
</lang>
Output:
<pre>
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</pre>
 
=={{header|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 <i>[dependencies] \n num="0.2.0";</i>
<langsyntaxhighlight Clang="rust">use num::Complex;
fn main() {
let n = 8;
Line 1,823 ⟶ 2,454:
println!("e^{:2}πi/{} ≈ {:>14.3}",2*k,n,z.powf(2.0*k as f64));
}
}</langsyntaxhighlight>
<pre>
e^ 0πi/8 ≈ 1.000+0.000i
Line 1,837 ⟶ 2,468:
=={{header|Scala}}==
Using [[Arithmetic/Complex#Scala|Complex]] class from task Arithmetic/Complex.
<langsyntaxhighlight lang="scala">def rootsOfUnity(n:Int)=for(k <- 0 until n) yield Complex.fromPolar(1.0, 2*math.Pi*k/n)</langsyntaxhighlight>
Usage:
<pre>rootsOfUnity(3) foreach println
Line 1,847 ⟶ 2,478:
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define pi (* 4 (atan 1)))
 
(do ((n 2 (+ n 1)))
Line 1,856 ⟶ 2,487:
(display " ")
(display (make-polar 1 (* 2 pi (/ k n)))))
(newline))</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "complex.s7i";
Line 1,875 ⟶ 2,506:
writeln;
end for;
end func;</langsyntaxhighlight>
Output:
<langsyntaxhighlight lang="seed7">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
Line 1,885 ⟶ 2,516:
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</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func roots_of_unity(n) {
n.of { |j|
exp(2i * Num.pi / n * j)
Line 1,897 ⟶ 2,528:
roots_of_unity(5).each { |c|
printf("%+.5f%+.5fi\n", c.reals)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,908 ⟶ 2,539:
 
=={{header|Sparkling}}==
<langsyntaxhighlight lang="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) {
Line 1,921 ⟶ 2,552:
foreach(unity_roots(6), function(k, v) {
printf("%.3f%+.3fi\n", v.re, v.im);
});</langsyntaxhighlight>
 
=={{header|Stata}}==
 
<langsyntaxhighlight lang="stata">n=7
exp(2i*pi()/n*(0::n-1))
1
Line 1,936 ⟶ 2,567:
6 | -.222520934 - .974927912i |
7 | .623489802 - .781831482i |
+-----------------------------+</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight Tcllang="tcl">package require Tcl 8.5
namespace import tcl::mathfunc::*
 
Line 1,951 ⟶ 2,582:
}
puts $row
}</langsyntaxhighlight>
 
=={{header|TI-89 BASIC}}==
<lang ti89b>cZeros(x^n - 1, x)</lang>
For n=3 in exact mode, the results are
<lang ti89b>{-1/2+√(3)/2*i, -1/2-√(3)/2*i, 1}</lang>
 
=={{header|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.
<lang Ursala>#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></lang>
The output is a list of lists of complex numbers.
<pre>
<
<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>></pre>
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Public Sub roots_of_unity()
For n = 2 To 9
Debug.Print n; "th roots of 1:"
Line 2,009 ⟶ 2,594:
Debug.Print
Next n
End Sub</langsyntaxhighlight>{{out}}
<pre> 2 th roots of 1:
Root 0: 1
Line 2,069 ⟶ 2,654:
Root 7: 0.17364817766693-0.984807753012208i
Root 8: 0.766044443118978-0.64278760968654i</pre>
 
=={{header|V (Vlang)}}==
{{trans|Ring}}
<syntaxhighlight lang="Zig">
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("")
}
</syntaxhighlight>
 
=={{header|Wren}}==
Line 2,074 ⟶ 2,676:
{{libheader|Wren-complex}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./complex" for Complex
import "./fmt" for Fmt
 
var roots = Fn.new { |n|
Line 2,085 ⟶ 2,687:
for (n in 2..5) {
Fmt.print("$d roots of 1:", n)
for (r in roots.call(n)) Fmt.print(" $17 0.14z", r)
}</langsyntaxhighlight>
 
{{out}}
<pre>
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
</pre>
 
=={{header|zkl}}==
{{trans|C}}
<langsyntaxhighlight lang="zkl">PI2:=(0.0).pi*2;
foreach n,i in ([1..9],n){
c:=s:=0;
Line 2,124 ⟶ 2,726:
print( (s==1 and "i") or (s==-1 and "-i" or (s and "%+.2gi" or"")).fmt(s));
print( (i==n-1) and "\n" or ", ");
}</langsyntaxhighlight>
{{out}}
<pre>
1,983

edits