Roots of unity: Difference between revisions

→‎{{header|Ada}}: Ada example fixed
(→‎{{header|C}}: Syntax highlighting + minor mods)
(→‎{{header|Ada}}: Ada example fixed)
Line 2:
 
=={{header|Ada}}==
<ada>
{{incorrect|Ada|It should print all n roots, either individually or by explicitly labeling conjugate pairs (x ± yi).}}
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Complex_Types; use Ada.Numerics.Complex_Types;
 
procedure Roots_Of_Unity is
with Ada.Text_Io; use Ada.Text_Io;
Root : Complex;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
begin
with Ada.Numerics.Generic_Complex_Types;
for N in 2..10 loop
with Ada.Numerics.Generic_Complex_Elementary_Functions;
Put_Line ("N =" & Integer'Image (N));
with Ada.Text_Io.Complex_Io;
for K in 0..N - 1 loop
Root :=
procedure Roots_Of_Unity is
Compose_From_Polar
type Real is digits 10;
( Modulus => 1.0,
package Complex_Type is new Ada.Numerics.Generic_Complex_Types(Real);
Argument => Float (K),
use Complex_Type;
Cycle => Float (N)
package Complex_Functions is new Ada.Numerics.Generic_Complex_Elementary_Functions(Complex_Type);
use Complex_Functions );
Put_Line
package Complex_Io is new Ada.Text_Io.Complex_Io(Complex_Type);
( " k =" & Integer'Image (K) & ", "
Factor : Complex;
& Float'Image (Re (Root)) & " +"
begin
& Float'Image (Im (Root)) & "i"
for Root in 2..10 loop
Put(Item => Root, Width => 4);
end Put(" ")loop;
for K in 1..Root/2end loop;
end Roots_Of_Unity;
Factor := Exp(Real(K) * 2.0 * Ada.Numerics.Pi / Real(Root));
</ada>
Complex_Io.Put(Item => Factor, Fore => 2, Aft => 4, Exp => 0);
[[Ada]] provides a direct implementation of polar composition of complex numbers ''x e''<sup>2&pi;''i y''</sup>. The function Compose_From_Polar is used to compose roots. The third argument of the function is the cycle. Instead of the standard cycle 2&pi;, N is used. Sample output:
end loop;
<pre style="height:30ex;overflow:scroll">
New_Line;
N = 2
end loop;
k = 0, 1.00000E+00 + 0.00000E+00i
end Roots_Of_Unity;
k = 1, -1.00000E+00 + 0.00000E+00i
Output:
N = 3
2 (-1.0000, 0.0000)
3k = (-0.5000, 1.00000E+00 + 0.8660)00000E+00i
4k (= 0.00001, 1-5.0000)(00000E-1.0000,01 0+ 8.0000)66025E-01i
5k (= 0.30902, 0.9511)(-05.8090,00000E-01 0+-8.5878)66025E-01i
N = 4
6 ( 0.5000, 0.8660)(-0.5000, 0.8660)(-1.0000, 0.0000)
7k (= 0.6235, 0 1.7818)(-0.2225,00000E+00 0.9749)(-0.9010,+ 0.4339)00000E+00i
k = 1, 0.00000E+00 + 1.00000E+00i
8 ( 0.7071, 0.7071)( 0.0000, 1.0000)(-0.7071, 0.7071)(-1.0000, 0.0000)
k = 2, -1.00000E+00 + 0.00000E+00i
9 ( 0.7660, 0.6428)( 0.1736, 0.9848)(-0.5000, 0.8660)(-0.9397, 0.3420)
k = 3, 0.00000E+00 +-1.00000E+00i
10 ( 0.8090, 0.5878)( 0.3090, 0.9511)(-0.3090, 0.9511)(-0.8090, 0.5878)(-1.0000, 0.0000)
N = 5
k = 0, 1.00000E+00 + 0.00000E+00i
k = 1, 3.09017E-01 + 9.51057E-01i
k = 2, -8.09017E-01 + 5.87785E-01i
k = 3, -8.09017E-01 +-5.87785E-01i
k = 4, 3.09017E-01 +-9.51056E-01i
N = 6
k = 0, 1.00000E+00 + 0.00000E+00i
k = 1, 5.00000E-01 + 8.66025E-01i
k = 2, -5.00000E-01 + 8.66025E-01i
k = 3, -1.00000E+00 + 0.00000E+00i
k = 4, -5.00000E-01 +-8.66025E-01i
k = 5, 5.00000E-01 +-8.66025E-01i
N = 7
k = 0, 1.00000E+00 + 0.00000E+00i
k = 1, 6.23490E-01 + 7.81832E-01i
k = 2, -2.22521E-01 + 9.74928E-01i
k = 3, -9.00969E-01 + 4.33884E-01i
k = 4, -9.00969E-01 +-4.33884E-01i
k = 5, -2.22521E-01 +-9.74928E-01i
k = 6, 6.23490E-01 +-7.81831E-01i
N = 8
k = 0, 1.00000E+00 + 0.00000E+00i
k = 1, 7.07107E-01 + 7.07107E-01i
k = 2, 0.00000E+00 + 1.00000E+00i
k = 3, -7.07107E-01 + 7.07107E-01i
k = 4, -1.00000E+00 + 0.00000E+00i
k = 5, -7.07107E-01 +-7.07107E-01i
k = 6, 0.00000E+00 +-1.00000E+00i
k = 7, 7.07107E-01 +-7.07107E-01i
N = 9
k = 0, 1.00000E+00 + 0.00000E+00i
k = 1, 7.66044E-01 + 6.42788E-01i
k = 2, 1.73648E-01 + 9.84808E-01i
k = 3, -5.00000E-01 + 8.66025E-01i
k = 4, -9.39693E-01 + 3.42020E-01i
k = 5, -9.39693E-01 +-3.42020E-01i
k = 6, -5.00000E-01 +-8.66025E-01i
k = 7, 1.73648E-01 +-9.84808E-01i
k = 8, 7.66045E-01 +-6.42788E-01i
N = 10
k = 0, 1.00000E+00 + 0.00000E+00i
k = 1, 8.09017E-01 + 5.87785E-01i
k = 2, 3.09017E-01 + 9.51057E-01i
k = 3, -3.09017E-01 + 9.51056E-01i
k = 4, -8.09017E-01 + 5.87785E-01i
k = 5, -1.00000E+00 + 0.00000E+00i
k = 6, -8.09017E-01 +-5.87785E-01i
k = 7, -3.09017E-01 +-9.51057E-01i
k = 8, 3.09017E-01 +-9.51056E-01i
k = 9, 8.09017E-01 +-5.87785E-01i
</pre>
 
=={{header|ALGOL 68}}==