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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Roots of unity step by step in the Prolog 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 Prolog programming language

Source code in the prolog programming language

roots(N, Rs) :-
    succ(Pn, N), numlist(0, Pn, Ks),
    maplist(root(N), Ks, Rs).

root(N, M, R2) :-
    R0 is (2*M) rdiv N,  % multiple of PI
    (R0 > 1 -> R1 is R0 - 2; R1 = R0),  % adjust for principal values
    cis(R1, R2).

cis(0, 1) :- !.
cis(1, -1) :- !.
cis(1 rdiv 2, i) :- !.
cis(-1 rdiv 2, -i) :- !.
cis(-1 rdiv Q, exp(-i*pi/Q)) :- !.
cis(1 rdiv Q, exp(i*pi/Q)) :- !.
cis(P rdiv Q, exp(P*i*pi/Q)).


  

You may also check:How to resolve the algorithm Klarner-Rado sequence step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Pick random element step by step in the Swift programming language
You may also check:How to resolve the algorithm Polymorphism step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Fibonacci n-step number sequences step by step in the AWK programming language
You may also check:How to resolve the algorithm Perfect numbers step by step in the Arturo programming language