Jump to content

Roots of unity: Difference between revisions

m
(Dialects of BASIC moved to the BASIC section.)
m ((Dialects of BASIC moved to the BASIC section.))
Line 234:
LOOP WHILE angle < 2 * PI</syntaxhighlight>
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> @% = &20408
FOR n% = 2 TO 5
Line 253:
5: 1.0000 0.0000i, 0.3090 0.9511i, -0.8090 0.5878i, -0.8090 -0.5878i, 0.3090 -0.9511i
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">#define twopi 6.2831853071795864769252867665590057684
 
dim as uinteger m, n
dim as double real, imag, theta
input "n? ", n
 
for m = 0 to n-1
theta = m*twopi/n
real = cos(theta)
imag = sin(theta)
if imag >= 0 then
print using "#.##### + #.##### i"; real; imag
else
print using "#.##### - #.##### i"; real; -imag
end if
next m</syntaxhighlight>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">window 1, @"Roots of Unity", (0,0,1050,200)
 
long n, root
double real, imag
 
for n = 2 to 7
print n;":" ;
for root = 0 to n-1
real = cos( 2 * pi * root / n)
imag = sin( 2 * pi * root / n)
print using "-##.#####"; real;using "-##.#####"; imag; "i";
if root != n-1 then print ",";
next
print
next
 
HandleEvents</syntaxhighlight>
Output:
<pre>
2: 1.00000 0.00000i, -1.00000 0.00000i
3: 1.00000 0.00000i, -0.50000 0.86603i, -0.50000 -0.86603i
4: 1.00000 0.00000i, 0.00000 1.00000i, -1.00000 0.00000i, -0.00000 -1.00000i
5: 1.00000 0.00000i, 0.30902 0.95106i, -0.80902 0.58779i, -0.80902 -0.58779i, 0.30902 -0.95106i
6: 1.00000 0.00000i, 0.50000 0.86603i, -0.50000 0.86603i, -1.00000 0.00000i, -0.50000 -0.86603i, 0.50000 -0.86603i
7: 1.00000 0.00000i, 0.62349 0.78183i, -0.22252 0.97493i, -0.90097 0.43388i, -0.90097 -0.43388i, -0.22252 -0.97493i, 0.62349 -0.78183i
</pre>
 
==={{header|Liberty BASIC}}===
<syntaxhighlight lang="lb">WindowWidth =400
WindowHeight =400
 
'nomainwin
 
open "N'th Roots of One" for graphics_nsb_nf as #w
 
#w "trapclose [quit]"
 
for n =1 To 10
angle =0
#w "font arial 16 bold"
print n; "th roots."
#w "cls"
#w "size 1 ; goto 200 200 ; down ; color lightgray ; circle 150 ; size 10 ; set 200 200 ; size 2"
#w "up ; goto 200 0 ; down ; goto 200 400 ; up ; goto 0 200 ; down ; goto 400 200"
#w "up ; goto 40 20 ; down ; color black"
#w "font arial 6"
#w "\"; n; " roots of 1."
 
for i = 1 To n
x = cos( Radian( angle))
y = sin( Radian( angle))
 
print using( "##", i); ": ( " + using( "##.######", x);_
" +i *" +using( "##.######", y); ") or e^( i *"; i -1; " *2 *Pi/ "; n; ")"
 
#w "color "; 255 *i /n; " 0 "; 256 -255 *i /n
#w "up ; goto 200 200"
#w "down ; goto "; 200 +150 *x; " "; 200 -150 *y
#w "up ; goto "; 200 +165 *x; " "; 200 -165 *y
#w "\"; str$( i)
#w "up"
 
angle =angle +360 /n
 
next i
 
timer 500, [on]
wait
[on]
timer 0
next n
 
wait
 
[quit]
close #w
 
end
 
function Radian( theta)
Radian =theta *3.1415926535 /180
end function</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">OpenConsole()
For n = 2 To 10
angle = 0
PrintN(Str(n))
For i = 1 To n
x.f = Cos(Radian(angle))
y.f = Sin(Radian(angle))
PrintN( Str(i) + ": " + StrF(x, 6) + " / " + StrF(y, 6))
angle = angle + (360 / n)
Next
Next
Input()</syntaxhighlight>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">PI = 3.1415926535
FOR n = 2 TO 5
PRINT n;":" ;
FOR root = 0 TO n-1
real = COS(2*PI * root / n)
imag = SIN(2*PI * root / n)
PRINT using("-##.#####",real);using("-##.#####",imag);"i";
IF root <> n-1 then PRINT "," ;
NEXT
PRINT
NEXT
</syntaxhighlight>
Output:
<pre>
2: 1.00000 0.00000i, -1.00000 0.00000i
3: 1.00000 0.00000i, -0.50000 0.86603i, -0.50000 -0.86603i
4: 1.00000 0.00000i, 0.00000 1.00000i, -1.00000 0.00000i, 0.00000 -1.00000i
5: 1.00000 0.00000i, 0.30902 0.95106i, -0.80902 0.58779i, -0.80902 -0.58779i, 0.30902 -0.95106i</pre>
 
==={{header|TI-89 BASIC}}===
<syntaxhighlight lang="ti89b">cZeros(x^n - 1, x)</syntaxhighlight>
For n=3 in exact mode, the results are
<syntaxhighlight lang="ti89b">{-1/2+√(3)/2*i, -1/2-√(3)/2*i, 1}</syntaxhighlight>
 
=={{header|Ursala}}==
The roots function takes a number n to the nth root of -1, squares it, and iteratively makes a list of its first n powers (oblivious to roundoff error). Complex functions cpow and mul are used, which are called from the host system's standard C library.
<syntaxhighlight lang="ursala">#import std
#import nat
#import flo
 
roots = ~&htxPC+ c..mul:-0^*DlSiiDlStK9\iota c..mul@iiX+ c..cpow/-1.+ div/1.+ float
 
#cast %jLL
 
tests = roots* <1,2,3,4,5,6></syntaxhighlight>
The output is a list of lists of complex numbers.
<pre>
<
<1.000e+00-2.449e-16j>,
<
1.000e+00-2.449e-16j,
-1.000e+00+1.225e-16j>,
<
1.000e+00-8.327e-16j,
-5.000e-01+8.660e-01j,
-5.000e-01-8.660e-01j>,
<
1.000e+00-8.882e-16j,
2.220e-16+1.000e+00j,
-1.000e+00+4.441e-16j,
-6.661e-16-1.000e+00j>,
<
1.000e+00-5.551e-17j,
3.090e-01+9.511e-01j,
-8.090e-01+5.878e-01j,
-8.090e-01-5.878e-01j,
3.090e-01-9.511e-01j>,
<
1.000e+00-1.221e-15j,
5.000e-01+8.660e-01j,
-5.000e-01+8.660e-01j,
-1.000e+00+6.106e-16j,
-5.000e-01-8.660e-01j,
5.000e-01-8.660e-01j>></pre>
 
=={{header|C}}==
Line 599 ⟶ 781:
end do
end program unity</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">#define twopi 6.2831853071795864769252867665590057684
 
dim as uinteger m, n
dim as double real, imag, theta
input "n? ", n
 
for m = 0 to n-1
theta = m*twopi/n
real = cos(theta)
imag = sin(theta)
if imag >= 0 then
print using "#.##### + #.##### i"; real; imag
else
print using "#.##### - #.##### i"; real; -imag
end if
next m</syntaxhighlight>
 
=={{header|Frink}}==
Line 654 ⟶ 818:
<pre>
{1.0, -0.4999999999999998+0.8660254037844387i, -0.5000000000000004-0.8660254037844385i}
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">window 1, @"Roots of Unity", (0,0,1050,200)
 
long n, root
double real, imag
 
for n = 2 to 7
print n;":" ;
for root = 0 to n-1
real = cos( 2 * pi * root / n)
imag = sin( 2 * pi * root / n)
print using "-##.#####"; real;using "-##.#####"; imag; "i";
if root != n-1 then print ",";
next
print
next
 
HandleEvents</syntaxhighlight>
Output:
<pre>
2: 1.00000 0.00000i, -1.00000 0.00000i
3: 1.00000 0.00000i, -0.50000 0.86603i, -0.50000 -0.86603i
4: 1.00000 0.00000i, 0.00000 1.00000i, -1.00000 0.00000i, -0.00000 -1.00000i
5: 1.00000 0.00000i, 0.30902 0.95106i, -0.80902 0.58779i, -0.80902 -0.58779i, 0.30902 -0.95106i
6: 1.00000 0.00000i, 0.50000 0.86603i, -0.50000 0.86603i, -1.00000 0.00000i, -0.50000 -0.86603i, 0.50000 -0.86603i
7: 1.00000 0.00000i, 0.62349 0.78183i, -0.22252 0.97493i, -0.90097 0.43388i, -0.90097 -0.43388i, -0.22252 -0.97493i, 0.62349 -0.78183i
</pre>
 
Line 1,036 ⟶ 1,172:
 
</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">WindowWidth =400
WindowHeight =400
 
'nomainwin
 
open "N'th Roots of One" for graphics_nsb_nf as #w
 
#w "trapclose [quit]"
 
for n =1 To 10
angle =0
#w "font arial 16 bold"
print n; "th roots."
#w "cls"
#w "size 1 ; goto 200 200 ; down ; color lightgray ; circle 150 ; size 10 ; set 200 200 ; size 2"
#w "up ; goto 200 0 ; down ; goto 200 400 ; up ; goto 0 200 ; down ; goto 400 200"
#w "up ; goto 40 20 ; down ; color black"
#w "font arial 6"
#w "\"; n; " roots of 1."
 
for i = 1 To n
x = cos( Radian( angle))
y = sin( Radian( angle))
 
print using( "##", i); ": ( " + using( "##.######", x);_
" +i *" +using( "##.######", y); ") or e^( i *"; i -1; " *2 *Pi/ "; n; ")"
 
#w "color "; 255 *i /n; " 0 "; 256 -255 *i /n
#w "up ; goto 200 200"
#w "down ; goto "; 200 +150 *x; " "; 200 -150 *y
#w "up ; goto "; 200 +165 *x; " "; 200 -165 *y
#w "\"; str$( i)
#w "up"
 
angle =angle +360 /n
 
next i
 
timer 500, [on]
wait
[on]
timer 0
next n
 
wait
 
[quit]
close #w
 
end
 
function Radian( theta)
Radian =theta *3.1415926535 /180
end function</syntaxhighlight>
 
=={{header|Lua}}==
Line 1,527 ⟶ 1,607:
X = [1, exp(i*pi/4), i, exp(3*i*pi/4), -1, exp(... * ... * pi/4), -i, exp(... / ...)].
</pre>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">OpenConsole()
For n = 2 To 10
angle = 0
PrintN(Str(n))
For i = 1 To n
x.f = Cos(Radian(angle))
y.f = Sin(Radian(angle))
PrintN( Str(i) + ": " + StrF(x, 6) + " / " + StrF(y, 6))
angle = angle + (360 / n)
Next
Next
Input()</syntaxhighlight>
 
=={{header|Python}}==
Line 1,847 ⟶ 1,913:
[(1+0.0i), (-0.4999999999999998+0.8660254037844387i), (-0.5000000000000004-0.8660254037844384i)]
</pre>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">PI = 3.1415926535
FOR n = 2 TO 5
PRINT n;":" ;
FOR root = 0 TO n-1
real = COS(2*PI * root / n)
imag = SIN(2*PI * root / n)
PRINT using("-##.#####",real);using("-##.#####",imag);"i";
IF root <> n-1 then PRINT "," ;
NEXT
PRINT
NEXT
</syntaxhighlight>
Output:
<pre>
2: 1.00000 0.00000i, -1.00000 0.00000i
3: 1.00000 0.00000i, -0.50000 0.86603i, -0.50000 -0.86603i
4: 1.00000 0.00000i, 0.00000 1.00000i, -1.00000 0.00000i, 0.00000 -1.00000i
5: 1.00000 0.00000i, 0.30902 0.95106i, -0.80902 0.58779i, -0.80902 -0.58779i, 0.30902 -0.95106i</pre>
 
=={{header|Rust}}==
Line 2,006 ⟶ 2,052:
puts $row
}</syntaxhighlight>
 
=={{header|TI-89 BASIC}}==
<syntaxhighlight lang="ti89b">cZeros(x^n - 1, x)</syntaxhighlight>
For n=3 in exact mode, the results are
<syntaxhighlight lang="ti89b">{-1/2+√(3)/2*i, -1/2-√(3)/2*i, 1}</syntaxhighlight>
 
=={{header|Ursala}}==
The roots function takes a number n to the nth root of -1, squares it, and iteratively makes a list of its first n powers (oblivious to roundoff error). Complex functions cpow and mul are used, which are called from the host system's standard C library.
<syntaxhighlight lang="ursala">#import std
#import nat
#import flo
 
roots = ~&htxPC+ c..mul:-0^*DlSiiDlStK9\iota c..mul@iiX+ c..cpow/-1.+ div/1.+ float
 
#cast %jLL
 
tests = roots* <1,2,3,4,5,6></syntaxhighlight>
The output is a list of lists of complex numbers.
<pre>
<
<1.000e+00-2.449e-16j>,
<
1.000e+00-2.449e-16j,
-1.000e+00+1.225e-16j>,
<
1.000e+00-8.327e-16j,
-5.000e-01+8.660e-01j,
-5.000e-01-8.660e-01j>,
<
1.000e+00-8.882e-16j,
2.220e-16+1.000e+00j,
-1.000e+00+4.441e-16j,
-6.661e-16-1.000e+00j>,
<
1.000e+00-5.551e-17j,
3.090e-01+9.511e-01j,
-8.090e-01+5.878e-01j,
-8.090e-01-5.878e-01j,
3.090e-01-9.511e-01j>,
<
1.000e+00-1.221e-15j,
5.000e-01+8.660e-01j,
-5.000e-01+8.660e-01j,
-1.000e+00+6.106e-16j,
-5.000e-01-8.660e-01j,
5.000e-01-8.660e-01j>></pre>
 
=={{header|VBA}}==
2,130

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.