-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhorner.py
More file actions
31 lines (21 loc) · 857 Bytes
/
horner.py
File metadata and controls
31 lines (21 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# -*- coding: utf-8 -*-
""" Horner's method for polynomial evaluation
This script allows the user to evaluate a given polynomial in O(n) time
A polynomial 2x^2 + 3x -1 is given as a list of coefficients as: [2, 3, -1]
This file contains the following function:
* main - the main function of the script
"""
def main():
"""
Main function to evaluate the polynomial function
"""
coef = [1,0,0,-1,-10]
x = 2
# The algorithm initializes result as coefficient of x^n, where n is the degree of polynomial and then
# Repeatedly multiply result with x and add next coefficient to result
result = coef[0]
for i in range(1, len(coef)):
result = (result * x) + coef[i]
print(f'The function evaluate to : {result} for given x value: {x}')
if __name__ == '__main__':
main()