Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # Created by Enji
- def polymul(coefficientsF, coefficientsG):
- m = len(coefficientsF)
- n = len(coefficientsG)
- prod = []
- for i in range(m+n-1):
- prod.append(0)
- for i in range(m):
- for j in range(n):
- prod[i+j] += coefficientsF[i] * coefficientsG[j]
- return prod
- def convert2String(prod):
- p = len(prod)
- for i in range(p-1, -1, -1):
- print(prod[-i+(p-1)], end="")
- if(i != -1):
- print("x^%d "%(i), end="")
- if(prod[-i+(p-1)] > 0 or prod[-i+(p-1)] < 0):
- if(prod[-i+(p)] > 0):
- print("+ ", end="")
- def main():
- degreeF = int(input("Masukkan derajat f: "))
- coefficientsF = []
- for i in range(degreeF, -1, -1):
- coefficientF = int(input("Masukkan koefesien x^%d: "%(i)))
- coefficientsF.append(coefficientF)
- degreeG = int(input("Masukkan derajat g: "))
- coefficientsG = []
- for i in range(degreeG, -1, -1):
- coefficientG = int(input("Masukkan koefesien x^%d: "%(i)))
- coefficientsG.append(coefficientG)
- print("Hasil perkalian polinom:")
- result = polymul(coefficientsF, coefficientsG)
- convert2String(result)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement