How to resolve the algorithm Polynomial long division step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Polynomial long division step by step in the Nim programming language

Table of Contents

Problem Statement

Let us suppose a polynomial is represented by a vector,

x

{\displaystyle x}

(i.e., an ordered collection of coefficients) so that the

i

{\displaystyle i}

th element keeps the coefficient of

x

i

{\displaystyle x^{i}}

, and the multiplication by a monomial is a shift of the vector's elements "towards right" (injecting ones from left) followed by a multiplication of each element by the coefficient of the monomial. Then a pseudocode for the polynomial long division using the conventions described above could be: Note: vector * scalar multiplies each element of the vector by the scalar; vectorA - vectorB subtracts each element of the vectorB from the element of the vectorA with "the same index". The vectors in the pseudocode are zero-based.

Example for clarification

This example is from Wikipedia, but changed to show how the given pseudocode works.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Polynomial long division step by step in the Nim programming language

Source code in the nim programming language

const MinusInfinity = -1

type
  Polynomial = seq[int]
  Term = tuple[coeff, exp: int]

func degree(p: Polynomial): int =
  ## Return the degree of a polynomial.
  ## "p" is supposed to be normalized.
  result = if p.len > 0: p.len - 1 else: MinusInfinity

func normalize(p: var Polynomial) =
  ## Normalize a polynomial, removing useless zeroes.
  while p[^1] == 0: discard p.pop()

func `shr`(p: Polynomial; n: int): Polynomial =
  ## Shift a polynomial of "n" positions to the right.
  result.setLen(p.len + n)
  result[n..^1] = p

func `*=`(p: var Polynomial; n: int) =
  ## Multiply in place a polynomial by an integer.
  for item in p.mitems: item *= n
  p.normalize()

func `-=`(a: var Polynomial; b: Polynomial) =
  ## Substract in place a polynomial from another polynomial.
  for i, val in b: a[i] -= val
  a.normalize()

func longdiv(a, b: Polynomial): tuple[q, r: Polynomial] =
  ## Compute the long division of a polynomial by another.
  ## Return the quotient and the remainder as polynomials.
  result.r = a
  if b.degree < 0: raise newException(DivByZeroDefect, "divisor cannot be zero.")
  result.q.setLen(a.len)
  while (let k = result.r.degree - b.degree; k >= 0):
    var d = b shr k
    result.q[k] = result.r[^1] div d[^1]
    d *= result.q[k]
    result.r -= d
  result.q.normalize()

const Superscripts: array['0'..'9', string] = ["⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹"]

func superscript(n: Natural): string =
  ## Return the Unicode string to use to represent an exponent.
  if n == 1:
    return ""
  for d in $n:
    result.add(Superscripts[d])

func `$`(term: Term): string =
  ## Return the string representation of a term.
  if term.coeff == 0: "0"
  elif term.exp == 0: $term.coeff
  else:
    let base = 'x' & superscript(term.exp)
    if term.coeff == 1: base
    elif term.coeff == -1: '-' & base
    else: $term.coeff & base

func `$`(poly: Polynomial): string =
  ## return the string representation of a polynomial.
  for idx in countdown(poly.high, 0):
    let coeff = poly[idx]
    var term: Term = (coeff: coeff, exp: idx)
    if result.len == 0:
      result.add $term
    else:
      if coeff > 0:
        result.add '+'
        result.add $term
      elif coeff < 0:
        term.coeff = -term.coeff
        result.add '-'
        result.add $term


const
  N = @[-42, 0, -12, 1]
  D = @[-3, 1]

let (q, r) = longdiv(N, D)
echo "N = ", N
echo "D = ", D
echo "q = ", q
echo "r = ", r


  

You may also check:How to resolve the algorithm Monty Hall problem step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the Swift programming language
You may also check:How to resolve the algorithm Repeat step by step in the Pascal programming language
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the DWScript programming language
You may also check:How to resolve the algorithm Prime decomposition step by step in the GAP programming language