Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Problem in defining a function

I want to compute the sum $$\sum_{k=0}^m\sum_{i=k}^m\sum_{j=rk}^{rk+r-1}(j-rk)+\sum_{k=0}^m\sum_{i=k}^k \sum_{j=rk+r}^{rk+tk}(j-rk)$$ Using Sage, one can use the code

i,j,k,r,m,t,s=var('i j k r m t s') 
x1=sum(sum(sum(j-r*k,j,r*k,r*k+r-1),i,k,m),k,0,m)
y1=sum(sum(sum(j-r*k,j,r*k+r,r*k+t*k),i,k,k),k,1,m)
nY=(x1+y1).expand().collect(m);

Note that the case $k=0$ in the second sum is exclused. To verify the sum is exact, I have defined the following function which also compute nY.

def Ver(m,t,r):
    s=0
    for k in srange(m+1):
        for i in srange(k,m+1):
            for j in srange(r*k,r*k+r):
                s=s+j-r*k
    for k in srange(m+1):
        for i in srange(k,k+1):
            for j in srange(r*k+r,r*k+t*k+1):
                s=s+j-r*k
    return s

Normally the two methods must give the same vaule, but in the case (m,t,r)=(2,1,3) we have nY.subs(m=2,t=1,r=3)=16 and Ver(2,1,3)=18, I want to know where is the problem which gives the error ?.