How to resolve the algorithm Roots of unity step by step in the C programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Roots of unity step by step in the C programming language
Table of Contents
Problem Statement
The purpose of this task is to explore working with complex numbers.
Given n, find the nth roots of unity.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Roots of unity step by step in the C programming language
This C program generates a table of complex nth roots of unity for various values of n
and i
, where n
is the order of the root and i
is the index of the root.
- A complex number is represented in rectangular form in the program. A complex number
z
can be written asz = c + si
, wherec
ands
are real numbers, andi
is the imaginary unit (i^2 = -1
). - The program uses the
atan2
function from themath.h
library to calculate the value ofPI2
, which is 8 times the arctangent of 1, or approximately2 * pi
. - The main loop of the program iterates through
n
values from 1 to 9 andi
values from 0 ton-1
for eachn
. - For each pair of
n
andi
, the program calculates the values ofc
ands
based on the conditions given:- If
i
is 0,c
is set to 1. - If
n
is 4 timesi
,s
is set to 1. - If
n
is 2 timesi
,c
is set to -1. - If 3 times
n
is 4 timesi
,s
is set to -1. - Otherwise,
c
ands
are calculated usingcos
andsin
.
- If
- The program then prints the values of
c
ands
in the specified format:c
is printed if it is non-zero.s
is printed with a leading "+" if it is positive, a leading "-" if it is negative, or with no leading sign if it is zero. The imaginary unit "i" is appended tos
if its value is 1 or -1.
- The program prints a newline character after each row of the table.
Here is an example of the output produced by the program:
1
1, -i, -1, -i, 1
1, 1, -1, -1, 1
1, -1, -i, 1, -i
1, i, -1, -i, 1
1, -1, -i, i, 1
1, -i, -1, -i, 1, -1
1, 1, -1, -1, 1, 1
1, -1, -i, 1, -i, 1
1, i, -1, -i, 1, 1
1, -1, -i, i, 1, 1
Source code in the c programming language
#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;
}
You may also check:How to resolve the algorithm Sum to 100 step by step in the Scala programming language
You may also check:How to resolve the algorithm Sierpinski triangle step by step in the Tcl programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the ML programming language
You may also check:How to resolve the algorithm Sleep step by step in the OCaml programming language
You may also check:How to resolve the algorithm Function definition step by step in the Delphi programming language