Python Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List

PythonServer Side ProgrammingProgramming

When it is required to compute a polynomial equation when the coefficients of the polynomial are stored in a list, a simple ‘for’ loop can be used.

Below is the demonstration of the same −

Example

 Live Demo

my_polynomial = [2, 5, 3, 0]
num = 2
poly_len = len(my_polynomial)
my_result = 0
for i in range(poly_len):
   my_sum = my_polynomial[i]
   for j in range(poly_len - i - 1):
      my_sum = my_sum * num
   my_result = my_result + my_sum
print("The polynomial equation for the given list of co-efficients is :")
print(my_result)

Output

The polynomial equation for the given list of co-efficients is :
42

Explanation

  • A list is defined.

  • A number is specified, and the length of the list is assigned to a variable.

  • A result variable is declared as 0.

  • The length of the list is iterated over, and the sum is added to the number.

  • This is given as the output.

  • This is displayed on the console.

raja
Published on 19-Apr-2021 10:49:56
Advertisements