How to resolve the algorithm Roots of unity step by step in the Go programming language
How to resolve the algorithm Roots of unity step by step in the Go 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 Go programming language
This code in Go prints the roots of unity for the given value of n
which corresponds to the number of roots.
The roots of unity are complex numbers that satisfy the equation z^n = 1
for some integer n
. They are evenly distributed on the unit circle in the complex plane.
The main
function calls the roots
function for n
values ranging from 2 to 5, inclusive.
The roots
function creates a slice of complex numbers and then loops over the range of n
values, calculating the roots of unity for each value and storing them in the slice.
The roots
function uses the cmplx.Rect
function to create a complex number with a given magnitude and phase angle. The magnitude is set to 1, and the phase angle is set to 2*math.Pi*float64(i)/float64(n)
where i
is the loop index and n
is the number of roots.
The main
function then prints the roots of unity for each value of n
using the fmt.Printf
function. The format string " %18.15f\n"
specifies that the complex numbers should be printed with 18 characters of width, 15 of which are after the decimal point.
Here is an example of the output of the program:
2 roots of 1:
-1.000000000000000 +0.000000000000000i
1.000000000000000 +0.000000000000000i
3 roots of 1:
-0.500000000000000 -0.866025403784438i
1.000000000000000 +0.000000000000000i
-0.500000000000000 +0.866025403784438i
4 roots of 1:
0.000000000000000 -1.000000000000000i
1.000000000000000 +0.000000000000000i
-0.000000000000000 -1.000000000000000i
1.000000000000000 +0.000000000000000i
5 roots of 1:
-0.309016994374947 -0.951056516295154i
0.809016994374947 -0.587785252292473i
1.000000000000000 +0.000000000000000i
0.809016994374947 +0.587785252292473i
-0.309016994374947 +0.951056516295154i
Source code in the go programming language
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
}
You may also check:How to resolve the algorithm Permutations by swapping step by step in the REXX programming language
You may also check:How to resolve the algorithm Menu step by step in the Swift programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the Caché ObjectScript programming language
You may also check:How to resolve the algorithm Ray-casting algorithm step by step in the Phix programming language
You may also check:How to resolve the algorithm Playing cards step by step in the Liberty BASIC programming language