Code : Tout sélectionner
Procedure Fact(x.l)
If x > 1
res = 1
For k = 1 To x
res = res * k
Next k
Else
res = 1
EndIf
ProcedureReturn res
EndProcedure
Procedure BinomialCoefficient(n.b, p.b, mode.b)
Select mode
Case 0
If n = p
ProcedureReturn 1
ElseIf p = 1
ProcedureReturn n
Else
ProcedureReturn BinomialCoefficient(n-1, p-1, mode)+BinomialCoefficient(n-1, p, mode)
EndIf
Case 1
ProcedureReturn Fact(n)/(Fact(p)*Fact(n-p))
EndSelect
EndProcedure
Procedure.s BinomialTheorem(Exponant)
Static *coef
mode = 0
If Exponant < 14
mode = 1
EndIf
Midle.l = Round(Exponant/2, 1)
BufLen = 4*(Exponant+1)
If Midle*2 <> Exponant
Midle = Midle - 1
EndIf
*coef = ReAllocateMemory(*coef, BufLen)
For k = 0 To k = Midle
PokeL(*coef+4*k, BinomialCoefficient(Exponant, k, mode))
Last = 4*k
Next k
i = 0
For k = BufLen-4 To Last Step -4
PokeL(*coef+k, PeekL(*coef+i))
i = i + 4
Next k
a = Exponant
For b = 0 To Exponant
coef = PeekL(*coef+b*4)
tmpa.s = ""
If a > 1
tmpa.s = "a^"+Str(a)
ElseIf a > 0
tmpa.s = "a"
EndIf
tmpb.s = ""
If b > 1
tmpb.s = "b^"+Str(b)
ElseIf b > 0
tmpb.s = "b"
EndIf
If tmpa <> "" And tmpb <> ""
tmp.s = "("+tmpa+")*("+tmpb+")"
ElseIf tmpa <> "" And tmpb = ""
tmp.s = tmpa
ElseIf tmpa = "" And tmpb <> ""
tmp.s = tmpb
EndIf
If coef > 1
tmp.s = Str(coef)+"*"+tmp
EndIf
If string.s <> ""
string.s = string.s +"+"
EndIf
string.s = string.s +tmp.s
a = a - 1
Next b
ProcedureReturn string.s
EndProcedure