Вычисление формул для (a + b)^n треугольником Паскаля

 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
#!/bin/env python
# -*- coding: utf-8 -*-
import sys
try:
ex = int(sys.argv[1])
except IndexError:
print("Syntax: %s number" % sys.argv[0])
sys.exit(1)
def nextLine(k):
j = [1]
for i in range(1, len(k)):
j.append(k[i-1] + k[i])
j.append(1)
return j
def formula(ex):
k = [1]; i = 1
while i <= ex:
i += 1
k = nextLine(k)
f = "(a+b)^{%d}=" % ex
a = ex; b = 0
for i in range(len(k)):
#f += "%d*a^%d*b^%d +" % (i, a, b),
if (k[i] > 1): f += str(k[i])
if (a > 0): f += "a"
if (a > 1): f += "^{%d}" % a
if (b > 0): f += "b"
if (b > 1): f += "^{%d}" % b
if (i < len(k)-1):
f += "+"
a -= 1; b += 1
return f
print (
"""\\documentclass[12pt]{letter}
\\usepackage{mathtext}
\\begin{document}
\\LARGE$%s$
\\end{document}""" % formula(ex))