categories: computer science
Hello everyone,
Below you will find a video with a detailed explanation of the algorithm for Horner's scheme. Its operation, time complexity, and usefulness. Below you will also find ready-to-use code that you can copy if needed.
Code:
# recursive method:
def schematHorneraRekurencyjnie(wspolczynniki, stopien, x):
if stopien == 0:
return wspolczynniki[0]
else:
return schematHorneraRekurencyjnie(wspolczynniki, stopien-1, x) * x + wspolczynniki[stopien]
# 2x^2 + 3x + 1 = x(2x + 3) + 1
# 1 + schematRekurencyjny(wspolczynniki, 1, x)*x
# 1 + (schematRekurencyjny(wspolczynniki, 0, x) + 3)*x
# 1 + (2*x + 3)*x
# iterative method
def schematHorneraIteracyjnie(wspolczynniki, stopien, x):
if stopien == 0:
return wspolczynniki[0]
wynik = wspolczynniki[0]
for i in range(stopien):
wynik = wynik * x + wspolczynniki[i + 1]
return wynik
# result = (2*x + 3)*x + 1
wspolczynniki = [2, 3, 1]
stopien = 2
x = 2
print(str(schematHorneraIteracyjnie(wspolczynniki, stopien, x)))
print(str(schematHorneraRekurencyjnie(wspolczynniki, stopien, x)))
Video:
Thank you for reading!
Read more