How to resolve the algorithm Roots of unity step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

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 as z = c + si, where c and s are real numbers, and i is the imaginary unit (i^2 = -1).
  • The program uses the atan2 function from the math.h library to calculate the value of PI2, which is 8 times the arctangent of 1, or approximately 2 * pi.
  • The main loop of the program iterates through n values from 1 to 9 and i values from 0 to n-1 for each n.
  • For each pair of n and i, the program calculates the values of c and s based on the conditions given:
    • If i is 0, c is set to 1.
    • If n is 4 times i, s is set to 1.
    • If n is 2 times i, c is set to -1.
    • If 3 times n is 4 times i, s is set to -1.
    • Otherwise, c and s are calculated using cos and sin.
  • The program then prints the values of c and s 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 to s 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