35 lines
707 B
Python
35 lines
707 B
Python
|
def pascal(n):
|
||
|
if n == 0:
|
||
|
return [1]
|
||
|
p = [1,1]
|
||
|
for i in range(n-1):
|
||
|
pn = [1]
|
||
|
for j in range(len(p)-1):
|
||
|
pn.append(p[j]+p[j+1])
|
||
|
pn.append(1)
|
||
|
p = pn
|
||
|
return p
|
||
|
|
||
|
def binomial(n):
|
||
|
i = n
|
||
|
j = 0
|
||
|
for c in pascal(n):
|
||
|
yield (c,i,j)
|
||
|
i -= 1
|
||
|
j += 1
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
print("Form: (x+y)^n")
|
||
|
x = float(input("Insert x:"))
|
||
|
y = float(input("Insert y:"))
|
||
|
n = int(input("Insert n:"))
|
||
|
|
||
|
assert(n>0)
|
||
|
res = 0
|
||
|
s = []
|
||
|
for (c,i,j) in binomial(n):
|
||
|
s.append("%d*(%.2f)^%d*(%.2f)^%d" % (c,x,i,y,j))
|
||
|
res += c * x**i * y**j
|
||
|
print(" + ".join(s))
|
||
|
print("Result: %f" % res)
|