Roots of unity: Difference between revisions
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 9: | Line 9: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang="11l">F polar(r, theta) |
||
R r * (cos(theta) + sin(theta) * 1i) |
R r * (cos(theta) + sin(theta) * 1i) |
||
Line 16: | Line 16: | ||
L(nr) 2..10 |
L(nr) 2..10 |
||
print(nr‘ ’croots(nr))</ |
print(nr‘ ’croots(nr))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 32: | Line 32: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
with Ada.Float_Text_IO; use Ada.Float_Text_IO; |
with Ada.Float_Text_IO; use Ada.Float_Text_IO; |
||
with Ada.Numerics.Complex_Types; use Ada.Numerics.Complex_Types; |
with Ada.Numerics.Complex_Types; use Ada.Numerics.Complex_Types; |
||
Line 65: | Line 65: | ||
end loop; |
end loop; |
||
end loop; |
end loop; |
||
end Roots_Of_Unity;</ |
end Roots_Of_Unity;</syntaxhighlight> |
||
[[Ada]] provides a direct implementation of polar composition of complex numbers ''x e''<sup>2π''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π, N is used. Sample output: |
[[Ada]] provides a direct implementation of polar composition of complex numbers ''x e''<sup>2π''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π, N is used. Sample output: |
||
<pre style="height:25ex;overflow:scroll"> |
<pre style="height:25ex;overflow:scroll"> |
||
Line 137: | Line 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]}} |
{{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}} |
{{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}} |
||
< |
<syntaxhighlight lang="algol68">FORMAT complex fmt=$g(-6,4)"⊥"g(-6,4)$; |
||
FOR root FROM 2 TO 10 DO |
FOR root FROM 2 TO 10 DO |
||
printf(($g(4)$,root)); |
printf(($g(4)$,root)); |
||
Line 144: | Line 144: | ||
OD; |
OD; |
||
printf($l$) |
printf($l$) |
||
OD</ |
OD</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 160: | Line 160: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">rect: function [r,phi][ |
||
to :complex @[ r * cos phi, r * sin phi ] |
to :complex @[ r * cos phi, r * sin phi ] |
||
] |
] |
||
Line 168: | Line 168: | ||
loop 2..10 'nr -> |
loop 2..10 'nr -> |
||
print [pad to :string nr 3 "=>" join.with:", " to [:string] .format:".3f" roots nr]</ |
print [pad to :string nr 3 "=>" join.with:", " to [:string] .format:".3f" roots nr]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 184: | Line 184: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
ahk forum: [http://www.autohotkey.com/forum/post-276712.html#276712 discussion] |
ahk forum: [http://www.autohotkey.com/forum/post-276712.html#276712 discussion] |
||
< |
<syntaxhighlight lang="autohotkey">n := 8, a := 8*atan(1)/n |
||
Loop %n% |
Loop %n% |
||
i := A_Index-1, t .= cos(a*i) ((s:=sin(a*i))<0 ? " - i*" . -s : " + i*" . s) "`n" |
i := A_Index-1, t .= cos(a*i) ((s:=sin(a*i))<0 ? " - i*" . -s : " + i*" . s) "`n" |
||
Msgbox % t</ |
Msgbox % t</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f ROOTS_OF_UNITY.AWK |
# syntax: GAWK -f ROOTS_OF_UNITY.AWK |
||
BEGIN { |
BEGIN { |
||
Line 206: | Line 206: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 219: | Line 219: | ||
{{trans|Java}} |
{{trans|Java}} |
||
For high n's, this may repeat the root of 1 + 0*i. |
For high n's, this may repeat the root of 1 + 0*i. |
||
< |
<syntaxhighlight lang="qbasic"> CLS |
||
PI = 3.1415926# |
PI = 3.1415926# |
||
n = 5 'this can be changed for any desired n |
n = 5 'this can be changed for any desired n |
||
Line 231: | Line 231: | ||
angle = angle + (2 * PI) / n |
angle = angle + (2 * PI) / n |
||
'all the way around the circle at even intervals |
'all the way around the circle at even intervals |
||
LOOP WHILE angle < 2 * PI</ |
LOOP WHILE angle < 2 * PI</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic"> @% = &20408 |
||
FOR n% = 2 TO 5 |
FOR n% = 2 TO 5 |
||
PRINT STR$(n%) ": " ; |
PRINT STR$(n%) ": " ; |
||
Line 244: | Line 244: | ||
NEXT |
NEXT |
||
PRINT |
PRINT |
||
NEXT n%</ |
NEXT n%</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
<pre> |
<pre> |
||
Line 254: | Line 254: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <math.h> |
#include <math.h> |
||
Line 277: | Line 277: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 302: | Line 302: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>(1, 0) |
<pre>(1, 0) |
||
Line 309: | Line 309: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <complex> |
||
#include <cmath> |
#include <cmath> |
||
#include <iostream> |
#include <iostream> |
||
Line 324: | Line 324: | ||
std::cout << std::endl; |
std::cout << std::endl; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
Most of the effort here is in formatting the results, and the output is still a bit clumsy. |
Most of the effort here is in formatting the results, and the output is still a bit clumsy. |
||
< |
<syntaxhighlight lang="coffeescript"># Find the n nth-roots of 1 |
||
nth_roots_of_unity = (n) -> |
nth_roots_of_unity = (n) -> |
||
(complex_unit_vector(2*Math.PI*i/n) for i in [1..n]) |
(complex_unit_vector(2*Math.PI*i/n) for i in [1..n]) |
||
Line 355: | Line 355: | ||
console.log "---1 to the 1/#{n}" |
console.log "---1 to the 1/#{n}" |
||
for root in nth_roots_of_unity n |
for root in nth_roots_of_unity n |
||
console.log root.toString()</ |
console.log root.toString()</syntaxhighlight> |
||
output |
output |
||
<pre> |
<pre> |
||
Line 380: | Line 380: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun roots-of-unity (n) |
||
(loop for i below n |
(loop for i below n |
||
collect (cis (* pi (/ (* 2 i) n)))))</ |
collect (cis (* pi (/ (* 2 i) n)))))</syntaxhighlight> |
||
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>. |
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}}== |
=={{header|Crystal}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ruby">require "complex" |
||
def roots_of_unity(n) |
def roots_of_unity(n) |
||
Line 394: | Line 394: | ||
p roots_of_unity(3) |
p roots_of_unity(3) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Or alternative |
Or alternative |
||
< |
<syntaxhighlight lang="ruby"> |
||
def roots_of_unity(n) |
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)) } |
(0...n).map { |k| Complex.new(Math.cos(2 * Math::PI * k / n), Math.sin(2 * Math::PI * k / n)) } |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 408: | Line 408: | ||
=={{header|D}}== |
=={{header|D}}== |
||
Using std.complex: |
Using std.complex: |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.range, std.algorithm, std.complex; |
||
import std.math: PI; |
import std.math: PI; |
||
Line 418: | Line 418: | ||
foreach (immutable i; 1 .. 6) |
foreach (immutable i; 1 .. 6) |
||
writefln("#%d: [%(%5.2f, %)]", i, i.nthRoots); |
writefln("#%d: [%(%5.2f, %)]", i, i.nthRoots); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>#1: [ 1.00+ 0.00i] |
<pre>#1: [ 1.00+ 0.00i] |
||
Line 428: | Line 428: | ||
{{libheader| System.VarCmplx}} |
{{libheader| System.VarCmplx}} |
||
{{Trans|C#}} |
{{Trans|C#}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Roots_of_unity; |
program Roots_of_unity; |
||
Line 454: | Line 454: | ||
Writeln(num); |
Writeln(num); |
||
Readln; |
Readln; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 464: | Line 464: | ||
</pre> |
</pre> |
||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
(define (roots-1 n) |
(define (roots-1 n) |
||
(define theta (// (* 2 PI) n)) |
(define theta (// (* 2 PI) n)) |
||
Line 476: | Line 476: | ||
(roots-1 4) |
(roots-1 4) |
||
→ (1+0i 0+i -1+0i 0-i) |
→ (1+0i 0+i -1+0i 0-i) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
PROGRAM UNITY_ROOTS |
PROGRAM UNITY_ROOTS |
||
Line 500: | Line 500: | ||
UNTIL ANGLE>=2*π |
UNTIL ANGLE>=2*π |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
Note: Adapted from Qbasic version. π is the predefined constant Greek Pi. |
Note: Adapted from Qbasic version. π is the predefined constant Greek Pi. |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: math.functions prettyprint ; |
||
1 3 roots .</ |
1 3 roots .</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 518: | Line 518: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Complex numbers are not a native type in Forth, so we calculate the roots by hand. |
Complex numbers are not a native type in Forth, so we calculate the roots by hand. |
||
< |
<syntaxhighlight lang="forth">: f0. ( f -- ) |
||
fdup 0e 0.001e f~ if fdrop 0e then f. ; |
fdup 0e 0.001e f~ if fdrop 0e then f. ; |
||
: .roots ( n -- ) |
: .roots ( n -- ) |
||
Line 527: | Line 527: | ||
3 set-precision |
3 set-precision |
||
5 .roots</ |
5 .roots</syntaxhighlight> |
||
{{libheader|Forth Scientific Library}} |
{{libheader|Forth Scientific Library}} |
||
On the other hand, complex numbers are implemented by the FSL. |
On the other hand, complex numbers are implemented by the FSL. |
||
{{works with|gforth|0.7.9_20170308}} |
{{works with|gforth|0.7.9_20170308}} |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="forth">require fsl-util.fs |
||
require fsl/complex.fs |
require fsl/complex.fs |
||
Line 549: | Line 549: | ||
LOOP ; |
LOOP ; |
||
3 SET-PRECISION |
3 SET-PRECISION |
||
5 .roots</ |
5 .roots</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
===Sin/Cos + Scalar Loop=== |
===Sin/Cos + Scalar Loop=== |
||
{{works with|Fortran|ISO Fortran 90 and later}} |
{{works with|Fortran|ISO Fortran 90 and later}} |
||
< |
<syntaxhighlight lang="fortran">PROGRAM Roots |
||
COMPLEX :: root |
COMPLEX :: root |
||
Line 572: | Line 572: | ||
END DO |
END DO |
||
END PROGRAM Roots</ |
END PROGRAM Roots</syntaxhighlight> |
||
Output |
Output |
||
2: +1.0000+0.0000j -1.0000+0.0000j |
2: +1.0000+0.0000j -1.0000+0.0000j |
||
Line 583: | Line 583: | ||
===Exp + Array-valued Statement=== |
===Exp + Array-valued Statement=== |
||
{{works with|Fortran|ISO Fortran 90 and later}} |
{{works with|Fortran|ISO Fortran 90 and later}} |
||
< |
<syntaxhighlight lang="fortran">program unity |
||
real, parameter :: pi = 3.141592653589793 |
real, parameter :: pi = 3.141592653589793 |
||
complex, parameter :: i = (0, 1) |
complex, parameter :: i = (0, 1) |
||
Line 597: | Line 597: | ||
write(*,*) |
write(*,*) |
||
end do |
end do |
||
end program unity</ |
end program unity</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">#define twopi 6.2831853071795864769252867665590057684 |
||
dim as uinteger m, n |
dim as uinteger m, n |
||
Line 615: | Line 615: | ||
print using "#.##### - #.##### i"; real; -imag |
print using "#.##### - #.##### i"; real; -imag |
||
end if |
end if |
||
next m</ |
next m</syntaxhighlight> |
||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
Calculates the angles in degrees, since Frink will use rational arithmetic (exact) |
Calculates the angles in degrees, since Frink will use rational arithmetic (exact) |
||
<syntaxhighlight lang="frink"> |
|||
<lang Frink> |
|||
roots[n] := |
roots[n] := |
||
{ |
{ |
||
Line 632: | Line 632: | ||
a |
a |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 643: | Line 643: | ||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
FunL has built-in support for complex numbers. <code>i</code> is predefined to represent the imaginary unit. |
FunL has built-in support for complex numbers. <code>i</code> is predefined to represent the imaginary unit. |
||
< |
<syntaxhighlight lang="funl">import math.{exp, Pi} |
||
def rootsOfUnity( n ) = {exp( 2Pi i k/n ) | k <- 0:n} |
def rootsOfUnity( n ) = {exp( 2Pi i k/n ) | k <- 0:n} |
||
println( rootsOfUnity(3) )</ |
println( rootsOfUnity(3) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 656: | Line 656: | ||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |
||
< |
<syntaxhighlight lang="futurebasic">window 1, @"Roots of Unity", (0,0,1050,200) |
||
long n, root |
long n, root |
||
Line 672: | Line 672: | ||
next |
next |
||
HandleEvents</ |
HandleEvents</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 684: | Line 684: | ||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
< |
<syntaxhighlight lang="gap">roots := n -> List([0 .. n-1], k -> E(n)^k); |
||
r:=roots(7); |
r:=roots(7); |
||
Line 690: | Line 690: | ||
List(r, x -> x^7); |
List(r, x -> x^7); |
||
# [ 1, 1, 1, 1, 1, 1, 1 ]</ |
# [ 1, 1, 1, 1, 1, 1, 1 ]</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 716: | Line 716: | ||
} |
} |
||
return r |
return r |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>2 roots of 1: |
<pre>2 roots of 1: |
||
Line 739: | Line 739: | ||
=={{header|Groovy}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="groovy">/** The following closure creates a list of n evenly-spaced points around the unit circle, |
||
* useful in FFT calculations, among other things */ |
* useful in FFT calculations, among other things */ |
||
def rootsOfUnity = { n -> |
def rootsOfUnity = { n -> |
||
Line 745: | Line 745: | ||
Complex.fromPolar(1, 2 * Math.PI * it / n) |
Complex.fromPolar(1, 2 * Math.PI * it / n) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Test program: |
Test program: |
||
< |
<syntaxhighlight lang="groovy">def tol = 0.000000001 // tolerance: acceptable "wrongness" to account for rounding error |
||
((1..6) + [16]). each { n -> |
((1..6) + [16]). each { n -> |
||
Line 760: | Line 760: | ||
assert rou.every { (it.rho - 1) < tol } : 'all roots should have magnitude 1' |
assert rou.every { (it.rho - 1) < tol } : 'all roots should have magnitude 1' |
||
println() |
println() |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre style="height:25ex;overflow:scroll;">rootsOfUnity(1): |
<pre style="height:25ex;overflow:scroll;">rootsOfUnity(1): |
||
Line 814: | Line 814: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.Complex (Complex, cis) |
||
rootsOfUnity :: (Enum a, Floating a) => a -> [Complex a] |
rootsOfUnity :: (Enum a, Floating a) => a -> [Complex a] |
||
Line 822: | Line 822: | ||
main :: IO () |
main :: IO () |
||
main = mapM_ print $ rootsOfUnity 3</ |
main = mapM_ print $ rootsOfUnity 3</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
< |
<syntaxhighlight lang="haskell">1.0 :+ 0.0 |
||
(-0.4999999999999998) :+ 0.8660254037844388 |
(-0.4999999999999998) :+ 0.8660254037844388 |
||
(-0.5000000000000004) :+ (-0.8660254037844384)</ |
(-0.5000000000000004) :+ (-0.8660254037844384)</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">procedure main() |
||
roots(10) |
roots(10) |
||
end |
end |
||
Line 840: | Line 840: | ||
procedure str_rep(k) |
procedure str_rep(k) |
||
return " " || cos(k) || "+" || sin(k) || "i" |
return " " || cos(k) || "+" || sin(k) || "i" |
||
end</ |
end</syntaxhighlight> |
||
Notes: |
Notes: |
||
* The [[:Category:Icon_Programming_Library|The Icon Programming Library]] implements a complex type but not a polar type |
* The [[:Category:Icon_Programming_Library|The Icon Programming Library]] implements a complex type but not a polar type |
||
Line 846: | Line 846: | ||
=={{header|IDL}}== |
=={{header|IDL}}== |
||
For some example <tt>n</tt>: |
For some example <tt>n</tt>: |
||
< |
<syntaxhighlight lang="idl">n = 5 |
||
print, exp( dcomplex( 0, 2*!dpi/n) ) ^ ( 1 + indgen(n) )</ |
print, exp( dcomplex( 0, 2*!dpi/n) ) ^ ( 1 + indgen(n) )</syntaxhighlight> |
||
Outputs: |
Outputs: |
||
< |
<syntaxhighlight lang="idl">( 0.30901699, 0.95105652)( -0.80901699, 0.58778525)( -0.80901699, -0.58778525)( 0.30901699, -0.95105652)( 1.0000000, -1.1102230e-16)</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j"> rou=: [: ^ 0j2p1 * i. % ] |
||
rou 4 |
rou 4 |
||
Line 858: | Line 858: | ||
rou 5 |
rou 5 |
||
1 0.309017j0.951057 _0.809017j0.587785 _0.809017j_0.587785 0.309017j_0.951057</ |
1 0.309017j0.951057 _0.809017j0.587785 _0.809017j_0.587785 0.309017j_0.951057</syntaxhighlight> |
||
The computation can also be written as a loop, shown here for comparison only. |
The computation can also be written as a loop, shown here for comparison only. |
||
< |
<syntaxhighlight lang="j">rou1=: 3 : 0 |
||
z=. 0 $ r=. ^ o. 0j2 % y [ e=. 1 |
z=. 0 $ r=. ^ o. 0j2 % y [ e=. 1 |
||
for. i.y do. |
for. i.y do. |
||
Line 867: | Line 867: | ||
end. |
end. |
||
z |
z |
||
)</ |
)</syntaxhighlight> |
||
=={{header|Java}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="java">import java.util.Locale; |
||
public class Test { |
public class Test { |
||
Line 899: | Line 899: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre>2: ( 1.000000, 0.000000) (-1.000000, 0.000000) |
<pre>2: ( 1.000000, 0.000000) (-1.000000, 0.000000) |
||
Line 907: | Line 907: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">function Root(angle) { |
||
with (Math) { this.r = cos(angle); this.i = sin(angle) } |
with (Math) { this.r = cos(angle); this.i = sin(angle) } |
||
} |
} |
||
Line 926: | Line 926: | ||
document.write('<br>') |
document.write('<br>') |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre>2: 1.00000+0.00000i, -1.00000+0.00000i |
<pre>2: 1.00000+0.00000i, -1.00000+0.00000i |
||
Line 937: | Line 937: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
Using the same example as in the Julia section, and representing x + i*y as [x,y]: |
Using the same example as in the Julia section, and representing x + i*y as [x,y]: |
||
< |
<syntaxhighlight lang="jq">def nthroots(n): |
||
(8 * (1|atan)) as $twopi |
(8 * (1|atan)) as $twopi |
||
| range(0;n) | (($twopi * .) / n) as $angle | [ ($angle | cos), ($angle | sin) ]; |
| range(0;n) | (($twopi * .) / n) as $angle | [ ($angle | cos), ($angle | sin) ]; |
||
nthroots(10)</ |
nthroots(10)</syntaxhighlight><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 |
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 959: | Line 959: | ||
user 0m0.004s |
user 0m0.004s |
||
sys 0m0.004s |
sys 0m0.004s |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">nthroots(n::Integer) = [ cospi(2k/n)+sinpi(2k/n)im for k = 0:n-1 ]</syntaxhighlight> |
||
(One could also use complex exponentials or other formulations.) For example, `nthroots(10)` gives: |
(One could also use complex exponentials or other formulations.) For example, `nthroots(10)` gives: |
||
<pre> |
<pre> |
||
Line 979: | Line 979: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">import java.lang.Math.* |
||
data class Complex(val r: Double, val i: Double) { |
data class Complex(val r: Double, val i: Double) { |
||
Line 999: | Line 999: | ||
(1..4).forEach { println(listOf(1) + unity_roots(it)) } |
(1..4).forEach { println(listOf(1) + unity_roots(it)) } |
||
println(listOf(1) + unity_roots(5.0)) |
println(listOf(1) + unity_roots(5.0)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1] |
<pre>[1] |
||
Line 1,008: | Line 1,008: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
// cleandisp just to display 0 when n < 10^-10 |
// cleandisp just to display 0 when n < 10^-10 |
||
{def cleandisp |
{def cleandisp |
||
Line 1,034: | Line 1,034: | ||
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) |
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}}== |
=={{header|Liberty BASIC}}== |
||
< |
<syntaxhighlight lang="lb">WindowWidth =400 |
||
WindowHeight =400 |
WindowHeight =400 |
||
Line 1,090: | Line 1,090: | ||
function Radian( theta) |
function Radian( theta) |
||
Radian =theta *3.1415926535 /180 |
Radian =theta *3.1415926535 /180 |
||
end function</ |
end function</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Complex numbers from the Lua implementation on the complex numbers page. |
Complex numbers from the Lua implementation on the complex numbers page. |
||
< |
<syntaxhighlight lang="lua">--defines addition, subtraction, negation, multiplication, division, conjugation, norms, and a conversion to strgs. |
||
complex = setmetatable({ |
complex = setmetatable({ |
||
__add = function(u, v) return complex(u.real + v.real, u.imag + v.imag) end, |
__add = function(u, v) return complex(u.real + v.real, u.imag + v.imag) end, |
||
Line 1,122: | Line 1,122: | ||
root = root * val |
root = root * val |
||
print(root .. "") |
print(root .. "") |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">RootsOfUnity := proc( n ) |
||
solve(z^n = 1, z); |
solve(z^n = 1, z); |
||
end proc:</ |
end proc:</syntaxhighlight> |
||
< |
<syntaxhighlight lang="maple">for i from 2 to 6 do |
||
printf( "%d: %a\n", i, [ RootsOfUnity(i) ] ); |
printf( "%d: %a\n", i, [ RootsOfUnity(i) ] ); |
||
end do;</ |
end do;</syntaxhighlight> |
||
Output: |
Output: |
||
< |
<syntaxhighlight lang="maple">2: [1, -1] |
||
3: [1, -1/2-1/2*I*3^(1/2), -1/2+1/2*I*3^(1/2)] |
3: [1, -1/2-1/2*I*3^(1/2), -1/2+1/2*I*3^(1/2)] |
||
4: [1, -1, I, -I] |
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)] |
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)]</ |
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)]</syntaxhighlight> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
Setting this up in Mathematica is easy, because it already handles complex numbers: |
Setting this up in Mathematica is easy, because it already handles complex numbers: |
||
< |
<syntaxhighlight lang="mathematica">RootsUnity[nthroot_Integer?Positive] := Table[Exp[2 Pi I i/nthroot], {i, 0, nthroot - 1}]</syntaxhighlight> |
||
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): |
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] |
<pre>RootsUnity[2] |
||
Line 1,188: | Line 1,188: | ||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
< |
<syntaxhighlight lang="matlab">function z = rootsOfUnity(n) |
||
assert(n >= 1,'n >= 1'); |
assert(n >= 1,'n >= 1'); |
||
z = roots([1 zeros(1,n-1) -1]); |
z = roots([1 zeros(1,n-1) -1]); |
||
end</ |
end</syntaxhighlight> |
||
Sample Output: |
Sample Output: |
||
< |
<syntaxhighlight lang="matlab">>> rootsOfUnity(3) |
||
ans = |
ans = |
||
Line 1,201: | Line 1,201: | ||
-0.500000000000000 + 0.866025403784439i |
-0.500000000000000 + 0.866025403784439i |
||
-0.500000000000000 - 0.866025403784439i |
-0.500000000000000 - 0.866025403784439i |
||
1.000000000000000 </ |
1.000000000000000 </syntaxhighlight> |
||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">solve(1 = x^n, x)</syntaxhighlight> |
||
Demonstration: |
Demonstration: |
||
< |
<syntaxhighlight lang="maxima">for n:1 thru 5 do display(solve(1 = x^n, x));</syntaxhighlight> |
||
Output: |
Output: |
||
< |
<syntaxhighlight lang="maxima">solve(1 = x, x) = [x = 1] |
||
solve(1 = x^2, x) = [x = -1, 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^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^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]</ |
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]</syntaxhighlight> |
||
=={{header|MiniScript}}== |
=={{header|MiniScript}}== |
||
<syntaxhighlight lang="miniscript"> |
|||
<lang MiniScript> |
|||
complexRoots = function(n) |
complexRoots = function(n) |
||
result = [] |
result = [] |
||
Line 1,230: | Line 1,230: | ||
for i in range(2,5) |
for i in range(2,5) |
||
print i + ": " + complexRoots(i).join(", ") |
print i + ": " + complexRoots(i).join(", ") |
||
end for</ |
end for</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,239: | Line 1,239: | ||
=={{header|МК-61/52}}== |
=={{header|МК-61/52}}== |
||
<lang>П0 0 П1 ИП1 sin ИП1 cos С/П 2 пи |
<syntaxhighlight lang="text">П0 0 П1 ИП1 sin ИП1 cos С/П 2 пи |
||
* ИП0 / ИП1 + П1 БП 03</ |
* ИП0 / ИП1 + П1 БП 03</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import complex, math, sequtils, strformat, strutils |
||
proc roots(n: Positive): seq[Complex64] = |
proc roots(n: Positive): seq[Complex64] = |
||
Line 1,256: | Line 1,256: | ||
let result = roots(nr).map(toString).join(", ") |
let result = roots(nr).map(toString).join(", ") |
||
echo &"{nr:2}: {result}" |
echo &"{nr:2}: {result}" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,270: | Line 1,270: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">open Complex |
||
let pi = 4. *. atan 1. |
let pi = 4. *. atan 1. |
||
Line 1,282: | Line 1,282: | ||
done; |
done; |
||
print_newline () |
print_newline () |
||
done</ |
done</syntaxhighlight> |
||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
< |
<syntaxhighlight lang="octave">for j = 2 : 10 |
||
printf("*** %d\n", j); |
printf("*** %d\n", j); |
||
for n = 1 : j |
for n = 1 : j |
||
Line 1,291: | Line 1,291: | ||
endfor |
endfor |
||
disp(""); |
disp(""); |
||
endfor</ |
endfor</syntaxhighlight> |
||
=={{header|OoRexx}}== |
=={{header|OoRexx}}== |
||
{{trans|REXX}} |
{{trans|REXX}} |
||
< |
<syntaxhighlight lang="oorexx">/*REXX program computes the K roots of unity (which include complex roots).*/ |
||
parse Version v |
parse Version v |
||
Say v |
Say v |
||
Line 1,320: | Line 1,320: | ||
if abs(x)<near0 then x=0 /*if near zero, then assume zero.*/ |
if abs(x)<near0 then x=0 /*if near zero, then assume zero.*/ |
||
return format(x,,frac)/1 /*fraction digits past dec point.*/ |
return format(x,,frac)/1 /*fraction digits past dec point.*/ |
||
::requires rxMath library</ |
::requires rxMath library</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>D:\>rexx nrootoo 5 |
<pre>D:\>rexx nrootoo 5 |
||
Line 1,332: | Line 1,332: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">vector(n,k,exp(2*Pi*I*k/n))</syntaxhighlight> |
||
<code>sqrtn()</code> can give the first n'th root, from which the others by multiplying or powering. |
<code>sqrtn()</code> can give the first n'th root, from which the others by multiplying or powering. |
||
< |
<syntaxhighlight lang="parigp">nth_roots(n) = my(z);sqrtn(1,n,&z); vector(n,i, z^i);</syntaxhighlight> |
||
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>. |
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,342: | Line 1,342: | ||
<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>.) |
<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>.) |
||
< |
<syntaxhighlight lang="parigp">sixth_root = quadgen(-3); /* 6th root of unity, exact */ |
||
vector(6,n, sixth_root^n) /* all the 6'th roots */</ |
vector(6,n, sixth_root^n) /* all the 6'th roots */</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{trans|Fortran}} |
{{trans|Fortran}} |
||
< |
<syntaxhighlight lang="pascal">Program Roots; |
||
var |
var |
||
Line 1,371: | Line 1,371: | ||
writeln; |
writeln; |
||
end; |
end; |
||
end.</ |
end.</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,388: | Line 1,388: | ||
The <code>root()</code> function returns a list of the N many N'th roots of any complex Z, in this case 1. |
The <code>root()</code> function returns a list of the N many N'th roots of any complex Z, in this case 1. |
||
< |
<syntaxhighlight lang="perl">use Math::Complex; |
||
foreach my $n (2 .. 10) { |
foreach my $n (2 .. 10) { |
||
Line 1,398: | Line 1,398: | ||
} |
} |
||
print "\n"; |
print "\n"; |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,414: | Line 1,414: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|AWK}} |
{{trans|AWK}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<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> |
<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> |
||
Line 1,425: | Line 1,425: | ||
<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: #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> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre style="font-size: 10px"> |
<pre style="font-size: 10px"> |
||
Line 1,441: | Line 1,441: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="picolisp">(load "@lib/math.l") |
||
(for N (range 2 10) |
(for N (range 2 10) |
||
Line 1,455: | Line 1,455: | ||
" " ) ) |
" " ) ) |
||
(inc 'Angle (*/ 2 pi N)) ) |
(inc 'Angle (*/ 2 pi N)) ) |
||
(prinl) ) )</ |
(prinl) ) )</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pl/i">complex_roots: |
||
procedure (N); |
procedure (N); |
||
declare N fixed binary nonassignable; |
declare N fixed binary nonassignable; |
||
Line 1,479: | Line 1,479: | ||
-0.30901709-0.95105648I |
-0.30901709-0.95105648I |
||
0.30901712-0.95105648I |
0.30901712-0.95105648I |
||
0.80901724-0.58778494I </ |
0.80901724-0.58778494I </syntaxhighlight> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Solves the roots of unity symbolically, as complex powers of e. |
Solves the roots of unity symbolically, as complex powers of e. |
||
<syntaxhighlight lang="prolog"> |
|||
<lang Prolog> |
|||
roots(N, Rs) :- |
roots(N, Rs) :- |
||
succ(Pn, N), numlist(0, Pn, Ks), |
succ(Pn, N), numlist(0, Pn, Ks), |
||
Line 1,500: | Line 1,500: | ||
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)). |
cis(P rdiv Q, exp(P*i*pi/Q)). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,528: | Line 1,528: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">OpenConsole() |
||
For n = 2 To 10 |
For n = 2 To 10 |
||
angle = 0 |
angle = 0 |
||
Line 1,539: | Line 1,539: | ||
Next |
Next |
||
Next |
Next |
||
Input()</ |
Input()</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python|3.7}} |
{{works with|Python|3.7}} |
||
< |
<syntaxhighlight lang="python">import cmath |
||
Line 1,573: | Line 1,573: | ||
for nr in range(2, 11): |
for nr in range(2, 11): |
||
print(nr, list(croots(nr)))</ |
print(nr, list(croots(nr)))</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>2 [1.00000, -1.00000] |
<pre>2 [1.00000, -1.00000] |
||
Line 1,586: | Line 1,586: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">for(j in 2:10) { |
||
r <- sprintf("%d: ", j) |
r <- sprintf("%d: ", j) |
||
for(n in 1:j) { |
for(n in 1:j) { |
||
Line 1,592: | Line 1,592: | ||
} |
} |
||
print(r) |
print(r) |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,607: | Line 1,607: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define (roots-of-unity n) |
(define (roots-of-unity n) |
||
(for/list ([k n]) |
(for/list ([k n]) |
||
(make-polar 1 (* k (/ (* 2 pi) n)))))</ |
(make-polar 1 (* k (/ (* 2 pi) n)))))</syntaxhighlight> |
||
Will produce a list of roots, for example: |
Will produce a list of roots, for example: |
||
<pre> |
<pre> |
||
Line 1,623: | Line 1,623: | ||
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>. |
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>. |
||
<lang |
<syntaxhighlight lang="raku" line>constant n = 10; |
||
for ^n -> \k { |
for ^n -> \k { |
||
say cis(k*τ/n); |
say cis(k*τ/n); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,647: | Line 1,647: | ||
Note: this REXX version only ''displays'' '''5''' significant digits past the decimal point, but this can be overridden by specifying the 2<sup>nd</sup> argument when invoking the REXX program. |
Note: this REXX version only ''displays'' '''5''' significant digits past the decimal point, but this can be overridden by specifying the 2<sup>nd</sup> argument when invoking the REXX program. |
||
(See the value of the REXX variable '''frac''', 5<sup>th</sup> line). |
(See the value of the REXX variable '''frac''', 5<sup>th</sup> line). |
||
< |
<syntaxhighlight 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. */ |
numeric digits length( pi() ) - length(.) /*use number of decimal digits in pi. */ |
||
parse arg n frac . /*get optional arguments from the C.L. */ |
parse arg n frac . /*get optional arguments from the C.L. */ |
||
Line 1,678: | Line 1,678: | ||
sin: procedure; parse arg x; x= r2r(x); numeric fuzz min(5, digits() - 3) |
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 |
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</ |
do k=2 by 2 until p=z; p=z; _= -_ * $x / (k*(k+1)); z= z + _; end; return z</syntaxhighlight> |
||
{{out|output|text= when using the input of: <tt> 5 </tt>}} |
{{out|output|text= when using the input of: <tt> 5 </tt>}} |
||
<pre> |
<pre> |
||
Line 1,800: | Line 1,800: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
decimals(4) |
decimals(4) |
||
for n = 2 to 5 |
for n = 2 to 5 |
||
Line 1,812: | Line 1,812: | ||
see nl |
see nl |
||
next |
next |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|RLaB}}== |
=={{header|RLaB}}== |
||
Line 1,818: | Line 1,818: | ||
:<math>x^n - 1 = 0.</math> |
:<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. |
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. |
||
< |
<syntaxhighlight lang="rlab">// specify polynomial |
||
>> n = 10; |
>> n = 10; |
||
>> a = zeros(1,n+1); a[1] = 1; a[n+1] = -1; |
>> a = zeros(1,n+1); a[1] = 1; a[n+1] = -1; |
||
Line 1,833: | Line 1,833: | ||
1 + 0i |
1 + 0i |
||
0.809016994 + 0.587785252i |
0.809016994 + 0.587785252i |
||
0.309016994 + 0.951056516i</ |
0.309016994 + 0.951056516i</syntaxhighlight> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">def roots_of_unity(n) |
||
(0...n).map {|k| Complex.polar(1, 2 * Math::PI * k / n)} |
(0...n).map {|k| Complex.polar(1, 2 * Math::PI * k / n)} |
||
end |
end |
||
p roots_of_unity(3)</ |
p roots_of_unity(3)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,848: | Line 1,848: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">PI = 3.1415926535 |
||
FOR n = 2 TO 5 |
FOR n = 2 TO 5 |
||
PRINT n;":" ; |
PRINT n;":" ; |
||
Line 1,859: | Line 1,859: | ||
PRINT |
PRINT |
||
NEXT |
NEXT |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,869: | Line 1,869: | ||
=={{header|Rust}}== |
=={{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> |
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> |
||
< |
<syntaxhighlight lang="rust">use num::Complex; |
||
fn main() { |
fn main() { |
||
let n = 8; |
let n = 8; |
||
Line 1,876: | Line 1,876: | ||
println!("e^{:2}πi/{} ≈ {:>14.3}",2*k,n,z.powf(2.0*k as f64)); |
println!("e^{:2}πi/{} ≈ {:>14.3}",2*k,n,z.powf(2.0*k as f64)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre> |
<pre> |
||
e^ 0πi/8 ≈ 1.000+0.000i |
e^ 0πi/8 ≈ 1.000+0.000i |
||
Line 1,890: | Line 1,890: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
Using [[Arithmetic/Complex#Scala|Complex]] class from task Arithmetic/Complex. |
Using [[Arithmetic/Complex#Scala|Complex]] class from task Arithmetic/Complex. |
||
< |
<syntaxhighlight lang="scala">def rootsOfUnity(n:Int)=for(k <- 0 until n) yield Complex.fromPolar(1.0, 2*math.Pi*k/n)</syntaxhighlight> |
||
Usage: |
Usage: |
||
<pre>rootsOfUnity(3) foreach println |
<pre>rootsOfUnity(3) foreach println |
||
Line 1,900: | Line 1,900: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang="scheme">(define pi (* 4 (atan 1))) |
||
(do ((n 2 (+ n 1))) |
(do ((n 2 (+ n 1))) |
||
Line 1,909: | Line 1,909: | ||
(display " ") |
(display " ") |
||
(display (make-polar 1 (* 2 pi (/ k n))))) |
(display (make-polar 1 (* 2 pi (/ k n))))) |
||
(newline))</ |
(newline))</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "float.s7i"; |
include "float.s7i"; |
||
include "complex.s7i"; |
include "complex.s7i"; |
||
Line 1,928: | Line 1,928: | ||
writeln; |
writeln; |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
Output: |
Output: |
||
< |
<syntaxhighlight lang="seed7">2: 1.0000+0.0000i -1.0000+0.0000i |
||
3: 1.0000+0.0000i -0.5000+0.8660i -0.5000-0.8660i |
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 |
4: 1.0000+0.0000i 0.0000+1.0000i -1.0000+0.0000i 0.0000-1.0000i |
||
Line 1,938: | Line 1,938: | ||
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 |
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 |
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</ |
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</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="ruby">func roots_of_unity(n) { |
||
n.of { |j| |
n.of { |j| |
||
exp(2i * Num.pi / n * j) |
exp(2i * Num.pi / n * j) |
||
Line 1,950: | Line 1,950: | ||
roots_of_unity(5).each { |c| |
roots_of_unity(5).each { |c| |
||
printf("%+.5f%+.5fi\n", c.reals) |
printf("%+.5f%+.5fi\n", c.reals) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,961: | Line 1,961: | ||
=={{header|Sparkling}}== |
=={{header|Sparkling}}== |
||
< |
<syntaxhighlight lang="sparkling">function unity_roots(n) { |
||
// nth-root(1) = cos(2 * k * pi / n) + i * sin(2 * k * pi / n) |
// nth-root(1) = cos(2 * k * pi / n) + i * sin(2 * k * pi / n) |
||
return map(range(n), function(idx, k) { |
return map(range(n), function(idx, k) { |
||
Line 1,974: | Line 1,974: | ||
foreach(unity_roots(6), function(k, v) { |
foreach(unity_roots(6), function(k, v) { |
||
printf("%.3f%+.3fi\n", v.re, v.im); |
printf("%.3f%+.3fi\n", v.re, v.im); |
||
});</ |
});</syntaxhighlight> |
||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
< |
<syntaxhighlight lang="stata">n=7 |
||
exp(2i*pi()/n*(0::n-1)) |
exp(2i*pi()/n*(0::n-1)) |
||
1 |
1 |
||
Line 1,989: | Line 1,989: | ||
6 | -.222520934 - .974927912i | |
6 | -.222520934 - .974927912i | |
||
7 | .623489802 - .781831482i | |
7 | .623489802 - .781831482i | |
||
+-----------------------------+</ |
+-----------------------------+</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
namespace import tcl::mathfunc::* |
namespace import tcl::mathfunc::* |
||
Line 2,004: | Line 2,004: | ||
} |
} |
||
puts $row |
puts $row |
||
}</ |
}</syntaxhighlight> |
||
=={{header|TI-89 BASIC}}== |
=={{header|TI-89 BASIC}}== |
||
< |
<syntaxhighlight lang="ti89b">cZeros(x^n - 1, x)</syntaxhighlight> |
||
For n=3 in exact mode, the results are |
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|Ursala}}== |
=={{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. |
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 nat |
||
#import flo |
#import flo |
||
Line 2,021: | Line 2,021: | ||
#cast %jLL |
#cast %jLL |
||
tests = roots* <1,2,3,4,5,6></ |
tests = roots* <1,2,3,4,5,6></syntaxhighlight> |
||
The output is a list of lists of complex numbers. |
The output is a list of lists of complex numbers. |
||
<pre> |
<pre> |
||
Line 2,053: | Line 2,053: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
< |
<syntaxhighlight lang="vb">Public Sub roots_of_unity() |
||
For n = 2 To 9 |
For n = 2 To 9 |
||
Debug.Print n; "th roots of 1:" |
Debug.Print n; "th roots of 1:" |
||
Line 2,062: | Line 2,062: | ||
Debug.Print |
Debug.Print |
||
Next n |
Next n |
||
End Sub</ |
End Sub</syntaxhighlight>{{out}} |
||
<pre> 2 th roots of 1: |
<pre> 2 th roots of 1: |
||
Root 0: 1 |
Root 0: 1 |
||
Line 2,127: | Line 2,127: | ||
{{libheader|Wren-complex}} |
{{libheader|Wren-complex}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/complex" for Complex |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 2,139: | Line 2,139: | ||
Fmt.print("$d roots of 1:", n) |
Fmt.print("$d roots of 1:", n) |
||
for (r in roots.call(n)) Fmt.print(" $17.14z", r) |
for (r in roots.call(n)) Fmt.print(" $17.14z", r) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,165: | Line 2,165: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="zkl">PI2:=(0.0).pi*2; |
||
foreach n,i in ([1..9],n){ |
foreach n,i in ([1..9],n){ |
||
c:=s:=0; |
c:=s:=0; |
||
Line 2,177: | Line 2,177: | ||
print( (s==1 and "i") or (s==-1 and "-i" or (s and "%+.2gi" or"")).fmt(s)); |
print( (s==1 and "i") or (s==-1 and "-i" or (s and "%+.2gi" or"")).fmt(s)); |
||
print( (i==n-1) and "\n" or ", "); |
print( (i==n-1) and "\n" or ", "); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
Revision as of 12:57, 28 August 2022
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 e2πi 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
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
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
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
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
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
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
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
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.
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
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
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
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
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}
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
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
Notes:
- The The Icon Programming Library implements a complex type but not a polar type
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)
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
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
/*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
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
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
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
(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(... / ...)].
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()
Python
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
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
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)]
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
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
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
}
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}
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>>
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
Wren
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(" $17.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
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
- Programming Tasks
- Arithmetic operations
- 11l
- Ada
- ALGOL 68
- Arturo
- AutoHotkey
- AWK
- BASIC
- BBC BASIC
- C
- C sharp
- C++
- CoffeeScript
- Common Lisp
- Crystal
- D
- Delphi
- System.VarCmplx
- EchoLisp
- ERRE
- Factor
- Forth
- Forth Scientific Library
- Fortran
- FreeBASIC
- Frink
- FunL
- FutureBasic
- GAP
- Go
- Groovy
- Haskell
- Icon
- Unicon
- IDL
- J
- Java
- JavaScript
- Jq
- Julia
- Kotlin
- Lambdatalk
- Liberty BASIC
- Lua
- Maple
- Mathematica
- Wolfram Language
- MATLAB
- Maxima
- MiniScript
- МК-61/52
- Nim
- OCaml
- Octave
- OoRexx
- PARI/GP
- Pascal
- Perl
- Phix
- PicoLisp
- PL/I
- Prolog
- PureBasic
- Python
- R
- Racket
- Raku
- REXX
- Ring
- RLaB
- Ruby
- Run BASIC
- Rust
- Scala
- Scheme
- Seed7
- Sidef
- Sparkling
- Stata
- Tcl
- TI-89 BASIC
- Ursala
- VBA
- Wren
- Wren-complex
- Wren-fmt
- Zkl