-
Notifications
You must be signed in to change notification settings - Fork 0
/
Schoolbook_algo.py
53 lines (40 loc) · 1.07 KB
/
Schoolbook_algo.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import time
def multiply(A, B, m, n):
prod = [0] * (m + n - 1)
# Multiply two polynomials term by term
# Take ever term of first polynomial
for i in range(m):
# Multiply the current term of first
# polynomial with every term of
# second polynomial.
for j in range(n):
prod[i + j] += A[i] * B[j]
return prod
# A utility function to print a polynomial
def printPoly(poly, n):
for i in range(n):
print(poly[i], end = "")
if (i != 0):
print("x^", i, end = "")
if (i != n - 1):
print(" + ", end = "")
# Driver Code
# The following array represents
# polynomial 5 + 10x^2 + 6x^3
A = [5, 0, 10, 6]
# The following array represents
# polynomial 1 + 2x + 4x^2
B = [1, 2, 4]
m = len(A)
n = len(B)
start=time.time()
print("First polynomial is ")
printPoly(A, m)
print("\nSecond polynomial is ")
printPoly(B, n)
prod = multiply(A, B, m, n)
print("\nProduct polynomial is ")
printPoly(prod, m+n-1)
end=time.time()
print()
print(end-start)