How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the BASIC programming language

Table of Contents

Problem Statement

A fast scheme for evaluating a polynomial such as: when is to arrange the computation as follows: And compute the result from the innermost brackets outwards as in this pseudocode: Task Description Cf. Formal power series

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the BASIC programming language

Source code in the basic programming language

100 HOME : REM  100 CLS for Chipmunk Basic and GW-BASIC
100 CLS : REM  100 HOME for Applesoft BASIC
110 X = 3
120 DIM COEFFS(3)
130 COEFFS(0) = -19
140 COEFFS(1) = 7
150 COEFFS(2) = -4
160 COEFFS(3) = 6
170 PRINT "Horner's algorithm for the polynomial "
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
190 ACCUM = 0
200 FOR I = 3 TO 0 STEP -1
210  ACCUM = (ACCUM*X)+COEFFS(I)
220 NEXT I
230 PRINT ACCUM
240 END


x = 3
dim coeficientes = {-19, 7, -4, 6}
print "Horner's algorithm for the polynomial ";
print "6*x^3 - 4*x^2 + 7*x - 19 when x = 3: ";
print AlgoritmoHorner(coeficientes, x)
end

function AlgoritmoHorner(coeffs, x)
	acumulador = 0
	for i = coeffs[?]-1 to 0 step -1
		acumulador = (acumulador * x) + coeffs[i]
	next i
	return acumulador
end function

100 CLS
110 x = 3
120 DIM coeffs(3)
130 coeffs(0) = -19
140 coeffs(1) = 7
150 coeffs(2) = -4
160 coeffs(3) = 6
170 PRINT "Horner's algorithm for the polynomial "
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
190 accum = 0
200 FOR i = UBOUND(coeffs,1) TO 0 STEP -1
210  accum = (accum*x)+coeffs(i)
220 NEXT i
230 PRINT accum
240 END


Public coeficientes As New Integer[4] 

Public Function AlgoritmoHorner(coeficientes As Integer[], x As Integer) As Integer 
  
  coeficientes[0] = -19 
  coeficientes[1] = 7 
  coeficientes[2] = -4 
  coeficientes[3] = 6 
  Dim i As Integer, acumulador As Integer = 0 

  For i = coeficientes.Count - 1 To 0 Step -1  
    acumulador = (acumulador * x) + coeficientes[i] 
  Next
  Return acumulador 
  
End Function 

Public Sub Main() 
  
  Dim x As Integer = 3 
  
  Print "Horner's algorithm for the polynomial 6*x^3 - 4*x^2 + 7*x - 19 when x = 3: "; 
  Print AlgoritmoHorner(coeficientes, x) 
  
End


100 CLS : REM  100 HOME for Applesoft BASIC
110 X = 3
120 DIM COEFFS(3)
130 COEFFS(0) = -19
140 COEFFS(1) = 7
150 COEFFS(2) = -4
160 COEFFS(3) = 6
170 PRINT "Horner's algorithm for the polynomial "
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
190 ACCUM = 0
200 FOR I = 3 TO 0 STEP -1
210  ACCUM = (ACCUM*X)+COEFFS(I)
220 NEXT I
230 PRINT ACCUM
240 END


20 LET X = 3
30 DIM C(3)
40 LET C(0) = -19
50 LET C(1) = 7
60 LET C(2) = -4
70 LET C(3) = 6
80 PRINT "HORNER'S ALGORITHM FOR THE POLYNOMIAL"
90 PRINT "6*X^3 - 4*X^2 + 7*X - 19 WHEN X = 3 : ";
100 LET A = 0
110 FOR I = 3 TO 0 STEP -1
120  LET A = (A*X)+C(I)
130 NEXT I
140 PRINT A
150 END


FUNCTION Horner (coeffs(), x)
    acumulador = 0
    FOR i = UBOUND(coeffs) TO LBOUND(coeffs) STEP -1
        acumulador = (acumulador * x) + coeffs(i)
    NEXT i
    Horner = acumulador
END FUNCTION

x = 3
DIM coeffs(3)
DATA -19, 7, -4, 6
FOR a = LBOUND(coeffs) TO UBOUND(coeffs)
READ coeffs(a)
NEXT a

PRINT "Horner's algorithm for the polynomial 6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
PRINT Horner(coeffs(), x)
END


x = 3
dim coeffs(4)
coeffs(0) = -19
coeffs(1) = 7
coeffs(2) = -4
coeffs(3) = 6
print "Horner's algorithm for the polynomial ";
print "6*x^3 - 4*x^2 + 7*x - 19 when x = 3: ";
print AlgoritmoHorner(coeffs, x)
end

sub AlgoritmoHorner(coeffs, x)
  local acumulador, i
  
  acumulador = 0
  for i = arraysize(coeffs(),1) to 0 step -1
    acumulador = (acumulador * x) + coeffs(i)
  next i
  return acumulador
end sub

  

You may also check:How to resolve the algorithm Terminal control/Dimensions step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Runtime evaluation/In an environment step by step in the Forth programming language
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Mutex step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Smarandache prime-digital sequence step by step in the Julia programming language