Вычислений, конечно, много лишних. Можно оптимизировать. def fact(m): f = 1 for i in range(1,m+1): f = f*i return f
def koef(n,k): a = fact(n) b = fact(k) c = fact(n-k) return a//(b*c)
print("Введите n:") n = int(input()) k = 0 while k <= n: print(koef(n,k),"*a^",n-k,"*b^",k,sep = '', end = '') k = k+1 if k <= n: print(" + ",end = '')
Answers & Comments
Verified answer
Вычислений, конечно, много лишних. Можно оптимизировать.def fact(m):
f = 1
for i in range(1,m+1):
f = f*i
return f
def koef(n,k):
a = fact(n)
b = fact(k)
c = fact(n-k)
return a//(b*c)
print("Введите n:")
n = int(input())
k = 0
while k <= n:
print(koef(n,k),"*a^",n-k,"*b^",k,sep = '', end = '')
k = k+1
if k <= n:
print(" + ",end = '')
Пример:
Введите n: 5
1*a^5*b^0 + 5*a^4*b^1 + 10*a^3*b^2 + 10*a^2*b^3 + 5*a^1*b^4 + 1*a^0*b^5