[Tutor] Calculating a math formula and finding errors

Michael Janssen Janssen@rz.uni-frankfurt.de
Fri Jan 10 10:06:02 2003


On Thu, 9 Jan 2003, Jens Kubieziel wrote:

> Hi,
>
> I found a nice formula in a math magazine [1]. Though I couldn't find a
> proof, I'm trying to compute some values of it. If this formula is right,
> both sides have to be equal. But within my code, both sides are not
> equal. So I think that I have an error in my code. Do you see the error
> and maybe general mistakes I made?
>
> formula in LaTeX [2]:
> \sum ^{n}_{i=0} \sum ^{n-i}_{j=0}|n+1-2(i+j)| \bigl( \begin{smallmatrix}
> n-1\\i \end{smallmatrix} \bigr) =2 \lfloor \frac{n+2}{2} \rfloor \bigl(
> \begin{smallmatrix} n+1\\ \lfloor \frac{n+1}{2} \rfloor \end{smallmatrix}
> \bigr) - (n+1)

smallmatrix is unknown to my latex. This has worked for me:

\begin{displaymath}
\sum ^{n}_{i=0} \sum ^{n-i}_{j=0}|n+1-2(i+j)|
{n-1 \choose i}   =2 \left\lfloor \frac{n+2}{2} \right\rfloor
 {n+1 \choose \lfloor \frac{n+1}{2} \rfloor}
 - (n+1)
\end{displaymath}


>
> Python code:
> #v+
> #! /usr/bin/python
>
> import math
>
> def facul(z):
>   if z ==1:
>     return 1
>   else:
>     return z*facul(z-1)
>
> def perm(x,y):
>   w = 1
>   for i in range(y+1,x+1):
>       w = w * i
>   return w/facul(x-y)
>
> def MBrechts(n):
>   b=0
>   for i in range(0,n):
>     for j in range(0,n-i):
>       betrag = n + 1 - (2*i) - (2*j)
>       if betrag < 0:
>         betrag = betrag * -1
>       prod = betrag * perm(n-i,j)

MBrechts is the left side, isn't it? It must be perm(n-1, i) to represent
latex: ${n-1 \choose i}$ (which is the binominalcoeffizient of n-1 and i)

But this won't make the output equal.

the "sigma-ranges" might (must? I don't know much about math notation) be
range(0,n+1) and range(0,n-i+1)  (both zeros aren't neccessary in python
but provided for the readers convenience).  (From the rest of the code I
guess, you're familar with "range" so this typo needn't be explained)

*This* correction (sigma-ranges) *without* the correction of perm(n-i,j)
gives equal output (Disclaimer: I've both perm and facul rewritten (since
i havn't understand your implementation of perm and wasn't familar with
recusive-facul ;-).

In case this doesn't already solve your problem, could you repost a
patched version (perhaps with english var-names to give the rest of
tutor-list a better chance :) and clean from rechts/links mess) so that
we can restart from a coherent version?

Would you give us some hints what's this formular is about? I havn't found
it on the webside. What does "perm" stands for?

Michael

>       b = b + prod
>   return b
>
> def MBlinks(n):
>   return 2 * math.floor((n+2)/2) * perm(n+1,math.floor((n+1)/2)) - n - 1
>
> for erg in range(1,12):
>   print "n = %d, Formel rechts = %d, Formel links = %d" % (erg,MBrechts(erg),MBlinks(erg))
> #v-
>
> Thanks for your recommendations
>
>
> Footnotes:
> ==========
> [1] print edition of http://www.wurzel.org
> [2] You can find a more readable version at
> http://www.kuwest.de/tmp/Bencze-Formel.pdf
> --
> Jens Kubieziel                                   mailto:jens@kubieziel.de
> If you cannot in the long run tell everyone what you have been doing,
> your doing was worthless.
> 		-- Edwim Schrodinger
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>