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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Polynomial long division step by step in the BBC BASIC 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 BBC BASIC programming language

Source code in the bbc programming language

      DIM N%(3) : N%() = -42, 0, -12, 1
      DIM D%(3) : D%() =  -3, 1,   0, 0
      DIM q%(3), r%(3)
      PROC_poly_long_div(N%(), D%(), q%(), r%())
      PRINT "Quotient = "; FNcoeff(q%(2)) "x^2" FNcoeff(q%(1)) "x" FNcoeff(q%(0))
      PRINT "Remainder = " ; r%(0)
      END
      
      DEF PROC_poly_long_div(N%(), D%(), q%(), r%())
      LOCAL d%(), i%, s%
      DIM d%(DIM(N%(),1))
      s% = FNdegree(N%()) - FNdegree(D%())
      IF s% >= 0 THEN
        q%() = 0
        WHILE s% >= 0
          FOR i% = 0 TO DIM(d%(),1) - s%
            d%(i%+s%) = D%(i%)
          NEXT
          q%(s%) = N%(FNdegree(N%())) DIV d%(FNdegree(d%()))
          d%() = d%() * q%(s%)
          N%() -= d%()
          s% = FNdegree(N%()) - FNdegree(D%())
        ENDWHILE
        r%() = N%()
      ELSE
        q%() = 0
        r%() = N%()
      ENDIF
      ENDPROC
      
      DEF FNdegree(a%())
      LOCAL i%
      i% = DIM(a%(),1)
      WHILE a%(i%)=0
        i% -= 1
        IF i%<0 EXIT WHILE
      ENDWHILE
      = i%
      
      DEF FNcoeff(n%)
      IF n%=0 THEN = ""
      IF n%<0 THEN = " - " + STR$(-n%)
      IF n%=1 THEN = " + "
      = " + " + STR$(n%)


  

You may also check:How to resolve the algorithm Date format step by step in the COBOL programming language
You may also check:How to resolve the algorithm Tree from nesting levels step by step in the Julia programming language
You may also check:How to resolve the algorithm Random number generator (device) step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Python programming language
You may also check:How to resolve the algorithm Gamma function step by step in the МК-61/52 programming language