Calculating $sumlimits_{i=1}^{n-1}sumlimits_{j=1}^{m-1} |mi-nj|$












2












$begingroup$


I'd like to calculate $f(n,m)=sumlimits_{i=1}^{n-1}sumlimits_{j=1}^{m-1} |mi-nj|$ for all $1 leq n leq N, 1 leq m leq M$. Straightforward brute force method runs in $O(N^2M^2)$ which is too slow. How to calculate all values in $O(NM)$?










share|cite|improve this question











$endgroup$












  • $begingroup$
    When $i=kj$, for some $k in mathbb{N}$, the sum can be simplified. For instance, for $k=1$, one has the term $left(1+2+cdots + min(n-1,m-1)right)|m-n|$
    $endgroup$
    – Alex Silva
    Nov 30 '18 at 9:33












  • $begingroup$
    I have a hunch the Euclidean algorithm could help: try computing $fleft(n,mright)$ using $fleft(n,m-nright)$.
    $endgroup$
    – darij grinberg
    Nov 30 '18 at 9:50












  • $begingroup$
    I'd look for a closed formula. E.g., one has $f(n,n)={2over3} n^2 - n^3 + {1over3}n^4$
    $endgroup$
    – Christian Blatter
    Nov 30 '18 at 11:15










  • $begingroup$
    How can this computation be $O(N^2M^2)$ ??? There are exactly $NM$ terms in the summation. There is certainly an $O(1)$ formula.
    $endgroup$
    – Yves Daoust
    Nov 30 '18 at 15:12
















2












$begingroup$


I'd like to calculate $f(n,m)=sumlimits_{i=1}^{n-1}sumlimits_{j=1}^{m-1} |mi-nj|$ for all $1 leq n leq N, 1 leq m leq M$. Straightforward brute force method runs in $O(N^2M^2)$ which is too slow. How to calculate all values in $O(NM)$?










share|cite|improve this question











$endgroup$












  • $begingroup$
    When $i=kj$, for some $k in mathbb{N}$, the sum can be simplified. For instance, for $k=1$, one has the term $left(1+2+cdots + min(n-1,m-1)right)|m-n|$
    $endgroup$
    – Alex Silva
    Nov 30 '18 at 9:33












  • $begingroup$
    I have a hunch the Euclidean algorithm could help: try computing $fleft(n,mright)$ using $fleft(n,m-nright)$.
    $endgroup$
    – darij grinberg
    Nov 30 '18 at 9:50












  • $begingroup$
    I'd look for a closed formula. E.g., one has $f(n,n)={2over3} n^2 - n^3 + {1over3}n^4$
    $endgroup$
    – Christian Blatter
    Nov 30 '18 at 11:15










  • $begingroup$
    How can this computation be $O(N^2M^2)$ ??? There are exactly $NM$ terms in the summation. There is certainly an $O(1)$ formula.
    $endgroup$
    – Yves Daoust
    Nov 30 '18 at 15:12














2












2








2


0



$begingroup$


I'd like to calculate $f(n,m)=sumlimits_{i=1}^{n-1}sumlimits_{j=1}^{m-1} |mi-nj|$ for all $1 leq n leq N, 1 leq m leq M$. Straightforward brute force method runs in $O(N^2M^2)$ which is too slow. How to calculate all values in $O(NM)$?










share|cite|improve this question











$endgroup$




I'd like to calculate $f(n,m)=sumlimits_{i=1}^{n-1}sumlimits_{j=1}^{m-1} |mi-nj|$ for all $1 leq n leq N, 1 leq m leq M$. Straightforward brute force method runs in $O(N^2M^2)$ which is too slow. How to calculate all values in $O(NM)$?







combinatorics summation algorithms closed-form computational-complexity






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Dec 17 '18 at 1:02









Batominovski

1




1










asked Nov 30 '18 at 7:47









Hang WuHang Wu

416210




416210












  • $begingroup$
    When $i=kj$, for some $k in mathbb{N}$, the sum can be simplified. For instance, for $k=1$, one has the term $left(1+2+cdots + min(n-1,m-1)right)|m-n|$
    $endgroup$
    – Alex Silva
    Nov 30 '18 at 9:33












  • $begingroup$
    I have a hunch the Euclidean algorithm could help: try computing $fleft(n,mright)$ using $fleft(n,m-nright)$.
    $endgroup$
    – darij grinberg
    Nov 30 '18 at 9:50












  • $begingroup$
    I'd look for a closed formula. E.g., one has $f(n,n)={2over3} n^2 - n^3 + {1over3}n^4$
    $endgroup$
    – Christian Blatter
    Nov 30 '18 at 11:15










  • $begingroup$
    How can this computation be $O(N^2M^2)$ ??? There are exactly $NM$ terms in the summation. There is certainly an $O(1)$ formula.
    $endgroup$
    – Yves Daoust
    Nov 30 '18 at 15:12


















  • $begingroup$
    When $i=kj$, for some $k in mathbb{N}$, the sum can be simplified. For instance, for $k=1$, one has the term $left(1+2+cdots + min(n-1,m-1)right)|m-n|$
    $endgroup$
    – Alex Silva
    Nov 30 '18 at 9:33












  • $begingroup$
    I have a hunch the Euclidean algorithm could help: try computing $fleft(n,mright)$ using $fleft(n,m-nright)$.
    $endgroup$
    – darij grinberg
    Nov 30 '18 at 9:50












  • $begingroup$
    I'd look for a closed formula. E.g., one has $f(n,n)={2over3} n^2 - n^3 + {1over3}n^4$
    $endgroup$
    – Christian Blatter
    Nov 30 '18 at 11:15










  • $begingroup$
    How can this computation be $O(N^2M^2)$ ??? There are exactly $NM$ terms in the summation. There is certainly an $O(1)$ formula.
    $endgroup$
    – Yves Daoust
    Nov 30 '18 at 15:12
















$begingroup$
When $i=kj$, for some $k in mathbb{N}$, the sum can be simplified. For instance, for $k=1$, one has the term $left(1+2+cdots + min(n-1,m-1)right)|m-n|$
$endgroup$
– Alex Silva
Nov 30 '18 at 9:33






$begingroup$
When $i=kj$, for some $k in mathbb{N}$, the sum can be simplified. For instance, for $k=1$, one has the term $left(1+2+cdots + min(n-1,m-1)right)|m-n|$
$endgroup$
– Alex Silva
Nov 30 '18 at 9:33














$begingroup$
I have a hunch the Euclidean algorithm could help: try computing $fleft(n,mright)$ using $fleft(n,m-nright)$.
$endgroup$
– darij grinberg
Nov 30 '18 at 9:50






$begingroup$
I have a hunch the Euclidean algorithm could help: try computing $fleft(n,mright)$ using $fleft(n,m-nright)$.
$endgroup$
– darij grinberg
Nov 30 '18 at 9:50














$begingroup$
I'd look for a closed formula. E.g., one has $f(n,n)={2over3} n^2 - n^3 + {1over3}n^4$
$endgroup$
– Christian Blatter
Nov 30 '18 at 11:15




$begingroup$
I'd look for a closed formula. E.g., one has $f(n,n)={2over3} n^2 - n^3 + {1over3}n^4$
$endgroup$
– Christian Blatter
Nov 30 '18 at 11:15












$begingroup$
How can this computation be $O(N^2M^2)$ ??? There are exactly $NM$ terms in the summation. There is certainly an $O(1)$ formula.
$endgroup$
– Yves Daoust
Nov 30 '18 at 15:12




$begingroup$
How can this computation be $O(N^2M^2)$ ??? There are exactly $NM$ terms in the summation. There is certainly an $O(1)$ formula.
$endgroup$
– Yves Daoust
Nov 30 '18 at 15:12










2 Answers
2






active

oldest

votes


















2












$begingroup$


We show the following is valid for positive integers $n,m$:
begin{align*}
sum_{i=1}^{n-1}sum_{j=1}^{m-1}|mi-nj|=frac{1}{6}left(2m^2n^2-3m^2n+m^2-3mn^2+3mn+n^2-left(gcd(m,n)right)^2right)
end{align*}




In the following we denote with $d=gcd(m,n)$.




We obtain
begin{align*}
color{blue}{sum_{i=1}^{n-1}}&color{blue}{sum_{j=1}^{m-1}|mi-nj|}tag{2}\
&=2sum_{i=1}^{n-1}sum_{j=1}^{lfloor mi/nrfloor}(mi-nj)tag{3}\
&=2msum_{i=1}^{n-1}isum_{j=1}^{lfloor mi/nrfloor}1-2nsum_{i=1}^{n-1}sum_{j=1}^{lfloor mi/nrfloor}j\
&=2msum_{i=1}^{n-1}ileftlfloorfrac{m}{n}irightrfloor
-nsum_{i=1}^{n-1}leftlfloorfrac{m}{n}irightrfloorleft(leftlfloorfrac{m}{n}irightrfloor+1right)tag{4}\
&=sum_{i=1}^{n-1}left(2mi-nleftlfloorfrac{m}{n}irightrfloor-nright)leftlfloorfrac{m}{n}irightrfloortag{5}\
&=sum_{i=1}^{n-1}left(2mi-nleft(frac{m}{n}i-left{frac{m}{n}iright}right)-nright)left(frac{m}{n}i-left{frac{m}{n}iright}right)tag{6}\
&=nsum_{i=1}^{n-1}left(frac{m^2}{n^2}i^2-left{frac{m}{n}iright}^2-frac{m}{n}i+left{frac{m}{n}iright}right)\
&=frac{m^2}{n}sum_{i=1}^{n-1}i^2-nsum_{i=1}^{n-1}left{frac{m}{n}iright}^2-msum_{i=1}^{n-1}i+nsum_{i=1}^{n-1}left{frac{m}{n}iright}\
&=frac{m^2}{n}frac{1}{6}(n-1)n(2n-1)-ndsum_{i=0}^{n/d-1}left(frac{d}{n}iright)^2\
&qquad -mfrac{1}{2}(n-1)n+ndsum_{i=0}^{n/d-1}left(frac{d}{n}iright)tag{7}\
&=frac{1}{6}m^2(n-1)(2n-1)-frac{d^3}{n}frac{1}{6}left(frac{n}{d}-1right)frac{n}{d}left(frac{2n}{d}-1right)\
&qquad-frac{1}{2}mn(n-1)+d^2frac{1}{2}left(frac{n}{d}-1right)frac{n}{d}tag{8}\
&,,color{blue}{=frac{1}{6}left(2m^2n^2-3m^2n+m^2-3mn^2+3mn+n^2-d^2right)}
end{align*}

and the claim (1) follows.




Comment:




  • In (3) we use that positive and negative parts in (2) correspond to each other.


  • In (4) we expand the inner sums.


  • In (5) we rearrange the terms and factor out $leftlfloorfrac{m}{n}irightrfloor$.


  • In (6) we rewrite the expression using the fractional part ${x}=x-lfloor xrfloor$ of $x$.


  • In (7) we expand the sums with linear and quadratic terms and we apply the identity
    begin{align*}
    sum_{i=1}^{n}fleft(left{frac{m}{n}iright}right)=dsum_{i=0}^{n/d-1}fleft(frac{d}{n}iright)
    end{align*}

    where $d=gcd(m,n)$.


  • In (8) we expand the sums and simplify the expression in the final step.







share|cite|improve this answer











$endgroup$





















    0












    $begingroup$

    Given $n$ and $mgeq n$ you can determine the numbers
    $$r_{nm}(j):=leftlfloor{m jover n}rightrfloorqquad(1leq jleq n-1)$$in $O(n)$ steps, and another $O(n)$ steps then compute
    $$f(n,m)=sum_{j=1}^{n-1}r_{nm}(j)bigl(2 m j-n(r_{nm}(j)+1)bigr) .tag{1}$$
    One arrives at this formula after observing that by symmetry it is sufficient to consider the lattice points $(j,k)$ below the line $y={mover n}x$. Look at these lattice points on the ordinate $x=j$. Their $y$-coordinates $k$ run through the set $[r_{nm}(j)]$. The"integrand" $p(j,k):=|m j-n k|=mj -n k$ produces an arithmetic progression on these lattice points. Therefore the sum of the $p$-values along the ordinate $x=j$ is their number $r_{nm}(j)$, times the arithmetic mean of the first and the last $p$-values. This leads to formula $(1)$.






    share|cite|improve this answer









    $endgroup$













      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      });
      });
      }, "mathjax-editing");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "69"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      noCode: true, onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3019794%2fcalculating-sum-limits-i-1n-1-sum-limits-j-1m-1-mi-nj%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2












      $begingroup$


      We show the following is valid for positive integers $n,m$:
      begin{align*}
      sum_{i=1}^{n-1}sum_{j=1}^{m-1}|mi-nj|=frac{1}{6}left(2m^2n^2-3m^2n+m^2-3mn^2+3mn+n^2-left(gcd(m,n)right)^2right)
      end{align*}




      In the following we denote with $d=gcd(m,n)$.




      We obtain
      begin{align*}
      color{blue}{sum_{i=1}^{n-1}}&color{blue}{sum_{j=1}^{m-1}|mi-nj|}tag{2}\
      &=2sum_{i=1}^{n-1}sum_{j=1}^{lfloor mi/nrfloor}(mi-nj)tag{3}\
      &=2msum_{i=1}^{n-1}isum_{j=1}^{lfloor mi/nrfloor}1-2nsum_{i=1}^{n-1}sum_{j=1}^{lfloor mi/nrfloor}j\
      &=2msum_{i=1}^{n-1}ileftlfloorfrac{m}{n}irightrfloor
      -nsum_{i=1}^{n-1}leftlfloorfrac{m}{n}irightrfloorleft(leftlfloorfrac{m}{n}irightrfloor+1right)tag{4}\
      &=sum_{i=1}^{n-1}left(2mi-nleftlfloorfrac{m}{n}irightrfloor-nright)leftlfloorfrac{m}{n}irightrfloortag{5}\
      &=sum_{i=1}^{n-1}left(2mi-nleft(frac{m}{n}i-left{frac{m}{n}iright}right)-nright)left(frac{m}{n}i-left{frac{m}{n}iright}right)tag{6}\
      &=nsum_{i=1}^{n-1}left(frac{m^2}{n^2}i^2-left{frac{m}{n}iright}^2-frac{m}{n}i+left{frac{m}{n}iright}right)\
      &=frac{m^2}{n}sum_{i=1}^{n-1}i^2-nsum_{i=1}^{n-1}left{frac{m}{n}iright}^2-msum_{i=1}^{n-1}i+nsum_{i=1}^{n-1}left{frac{m}{n}iright}\
      &=frac{m^2}{n}frac{1}{6}(n-1)n(2n-1)-ndsum_{i=0}^{n/d-1}left(frac{d}{n}iright)^2\
      &qquad -mfrac{1}{2}(n-1)n+ndsum_{i=0}^{n/d-1}left(frac{d}{n}iright)tag{7}\
      &=frac{1}{6}m^2(n-1)(2n-1)-frac{d^3}{n}frac{1}{6}left(frac{n}{d}-1right)frac{n}{d}left(frac{2n}{d}-1right)\
      &qquad-frac{1}{2}mn(n-1)+d^2frac{1}{2}left(frac{n}{d}-1right)frac{n}{d}tag{8}\
      &,,color{blue}{=frac{1}{6}left(2m^2n^2-3m^2n+m^2-3mn^2+3mn+n^2-d^2right)}
      end{align*}

      and the claim (1) follows.




      Comment:




      • In (3) we use that positive and negative parts in (2) correspond to each other.


      • In (4) we expand the inner sums.


      • In (5) we rearrange the terms and factor out $leftlfloorfrac{m}{n}irightrfloor$.


      • In (6) we rewrite the expression using the fractional part ${x}=x-lfloor xrfloor$ of $x$.


      • In (7) we expand the sums with linear and quadratic terms and we apply the identity
        begin{align*}
        sum_{i=1}^{n}fleft(left{frac{m}{n}iright}right)=dsum_{i=0}^{n/d-1}fleft(frac{d}{n}iright)
        end{align*}

        where $d=gcd(m,n)$.


      • In (8) we expand the sums and simplify the expression in the final step.







      share|cite|improve this answer











      $endgroup$


















        2












        $begingroup$


        We show the following is valid for positive integers $n,m$:
        begin{align*}
        sum_{i=1}^{n-1}sum_{j=1}^{m-1}|mi-nj|=frac{1}{6}left(2m^2n^2-3m^2n+m^2-3mn^2+3mn+n^2-left(gcd(m,n)right)^2right)
        end{align*}




        In the following we denote with $d=gcd(m,n)$.




        We obtain
        begin{align*}
        color{blue}{sum_{i=1}^{n-1}}&color{blue}{sum_{j=1}^{m-1}|mi-nj|}tag{2}\
        &=2sum_{i=1}^{n-1}sum_{j=1}^{lfloor mi/nrfloor}(mi-nj)tag{3}\
        &=2msum_{i=1}^{n-1}isum_{j=1}^{lfloor mi/nrfloor}1-2nsum_{i=1}^{n-1}sum_{j=1}^{lfloor mi/nrfloor}j\
        &=2msum_{i=1}^{n-1}ileftlfloorfrac{m}{n}irightrfloor
        -nsum_{i=1}^{n-1}leftlfloorfrac{m}{n}irightrfloorleft(leftlfloorfrac{m}{n}irightrfloor+1right)tag{4}\
        &=sum_{i=1}^{n-1}left(2mi-nleftlfloorfrac{m}{n}irightrfloor-nright)leftlfloorfrac{m}{n}irightrfloortag{5}\
        &=sum_{i=1}^{n-1}left(2mi-nleft(frac{m}{n}i-left{frac{m}{n}iright}right)-nright)left(frac{m}{n}i-left{frac{m}{n}iright}right)tag{6}\
        &=nsum_{i=1}^{n-1}left(frac{m^2}{n^2}i^2-left{frac{m}{n}iright}^2-frac{m}{n}i+left{frac{m}{n}iright}right)\
        &=frac{m^2}{n}sum_{i=1}^{n-1}i^2-nsum_{i=1}^{n-1}left{frac{m}{n}iright}^2-msum_{i=1}^{n-1}i+nsum_{i=1}^{n-1}left{frac{m}{n}iright}\
        &=frac{m^2}{n}frac{1}{6}(n-1)n(2n-1)-ndsum_{i=0}^{n/d-1}left(frac{d}{n}iright)^2\
        &qquad -mfrac{1}{2}(n-1)n+ndsum_{i=0}^{n/d-1}left(frac{d}{n}iright)tag{7}\
        &=frac{1}{6}m^2(n-1)(2n-1)-frac{d^3}{n}frac{1}{6}left(frac{n}{d}-1right)frac{n}{d}left(frac{2n}{d}-1right)\
        &qquad-frac{1}{2}mn(n-1)+d^2frac{1}{2}left(frac{n}{d}-1right)frac{n}{d}tag{8}\
        &,,color{blue}{=frac{1}{6}left(2m^2n^2-3m^2n+m^2-3mn^2+3mn+n^2-d^2right)}
        end{align*}

        and the claim (1) follows.




        Comment:




        • In (3) we use that positive and negative parts in (2) correspond to each other.


        • In (4) we expand the inner sums.


        • In (5) we rearrange the terms and factor out $leftlfloorfrac{m}{n}irightrfloor$.


        • In (6) we rewrite the expression using the fractional part ${x}=x-lfloor xrfloor$ of $x$.


        • In (7) we expand the sums with linear and quadratic terms and we apply the identity
          begin{align*}
          sum_{i=1}^{n}fleft(left{frac{m}{n}iright}right)=dsum_{i=0}^{n/d-1}fleft(frac{d}{n}iright)
          end{align*}

          where $d=gcd(m,n)$.


        • In (8) we expand the sums and simplify the expression in the final step.







        share|cite|improve this answer











        $endgroup$
















          2












          2








          2





          $begingroup$


          We show the following is valid for positive integers $n,m$:
          begin{align*}
          sum_{i=1}^{n-1}sum_{j=1}^{m-1}|mi-nj|=frac{1}{6}left(2m^2n^2-3m^2n+m^2-3mn^2+3mn+n^2-left(gcd(m,n)right)^2right)
          end{align*}




          In the following we denote with $d=gcd(m,n)$.




          We obtain
          begin{align*}
          color{blue}{sum_{i=1}^{n-1}}&color{blue}{sum_{j=1}^{m-1}|mi-nj|}tag{2}\
          &=2sum_{i=1}^{n-1}sum_{j=1}^{lfloor mi/nrfloor}(mi-nj)tag{3}\
          &=2msum_{i=1}^{n-1}isum_{j=1}^{lfloor mi/nrfloor}1-2nsum_{i=1}^{n-1}sum_{j=1}^{lfloor mi/nrfloor}j\
          &=2msum_{i=1}^{n-1}ileftlfloorfrac{m}{n}irightrfloor
          -nsum_{i=1}^{n-1}leftlfloorfrac{m}{n}irightrfloorleft(leftlfloorfrac{m}{n}irightrfloor+1right)tag{4}\
          &=sum_{i=1}^{n-1}left(2mi-nleftlfloorfrac{m}{n}irightrfloor-nright)leftlfloorfrac{m}{n}irightrfloortag{5}\
          &=sum_{i=1}^{n-1}left(2mi-nleft(frac{m}{n}i-left{frac{m}{n}iright}right)-nright)left(frac{m}{n}i-left{frac{m}{n}iright}right)tag{6}\
          &=nsum_{i=1}^{n-1}left(frac{m^2}{n^2}i^2-left{frac{m}{n}iright}^2-frac{m}{n}i+left{frac{m}{n}iright}right)\
          &=frac{m^2}{n}sum_{i=1}^{n-1}i^2-nsum_{i=1}^{n-1}left{frac{m}{n}iright}^2-msum_{i=1}^{n-1}i+nsum_{i=1}^{n-1}left{frac{m}{n}iright}\
          &=frac{m^2}{n}frac{1}{6}(n-1)n(2n-1)-ndsum_{i=0}^{n/d-1}left(frac{d}{n}iright)^2\
          &qquad -mfrac{1}{2}(n-1)n+ndsum_{i=0}^{n/d-1}left(frac{d}{n}iright)tag{7}\
          &=frac{1}{6}m^2(n-1)(2n-1)-frac{d^3}{n}frac{1}{6}left(frac{n}{d}-1right)frac{n}{d}left(frac{2n}{d}-1right)\
          &qquad-frac{1}{2}mn(n-1)+d^2frac{1}{2}left(frac{n}{d}-1right)frac{n}{d}tag{8}\
          &,,color{blue}{=frac{1}{6}left(2m^2n^2-3m^2n+m^2-3mn^2+3mn+n^2-d^2right)}
          end{align*}

          and the claim (1) follows.




          Comment:




          • In (3) we use that positive and negative parts in (2) correspond to each other.


          • In (4) we expand the inner sums.


          • In (5) we rearrange the terms and factor out $leftlfloorfrac{m}{n}irightrfloor$.


          • In (6) we rewrite the expression using the fractional part ${x}=x-lfloor xrfloor$ of $x$.


          • In (7) we expand the sums with linear and quadratic terms and we apply the identity
            begin{align*}
            sum_{i=1}^{n}fleft(left{frac{m}{n}iright}right)=dsum_{i=0}^{n/d-1}fleft(frac{d}{n}iright)
            end{align*}

            where $d=gcd(m,n)$.


          • In (8) we expand the sums and simplify the expression in the final step.







          share|cite|improve this answer











          $endgroup$




          We show the following is valid for positive integers $n,m$:
          begin{align*}
          sum_{i=1}^{n-1}sum_{j=1}^{m-1}|mi-nj|=frac{1}{6}left(2m^2n^2-3m^2n+m^2-3mn^2+3mn+n^2-left(gcd(m,n)right)^2right)
          end{align*}




          In the following we denote with $d=gcd(m,n)$.




          We obtain
          begin{align*}
          color{blue}{sum_{i=1}^{n-1}}&color{blue}{sum_{j=1}^{m-1}|mi-nj|}tag{2}\
          &=2sum_{i=1}^{n-1}sum_{j=1}^{lfloor mi/nrfloor}(mi-nj)tag{3}\
          &=2msum_{i=1}^{n-1}isum_{j=1}^{lfloor mi/nrfloor}1-2nsum_{i=1}^{n-1}sum_{j=1}^{lfloor mi/nrfloor}j\
          &=2msum_{i=1}^{n-1}ileftlfloorfrac{m}{n}irightrfloor
          -nsum_{i=1}^{n-1}leftlfloorfrac{m}{n}irightrfloorleft(leftlfloorfrac{m}{n}irightrfloor+1right)tag{4}\
          &=sum_{i=1}^{n-1}left(2mi-nleftlfloorfrac{m}{n}irightrfloor-nright)leftlfloorfrac{m}{n}irightrfloortag{5}\
          &=sum_{i=1}^{n-1}left(2mi-nleft(frac{m}{n}i-left{frac{m}{n}iright}right)-nright)left(frac{m}{n}i-left{frac{m}{n}iright}right)tag{6}\
          &=nsum_{i=1}^{n-1}left(frac{m^2}{n^2}i^2-left{frac{m}{n}iright}^2-frac{m}{n}i+left{frac{m}{n}iright}right)\
          &=frac{m^2}{n}sum_{i=1}^{n-1}i^2-nsum_{i=1}^{n-1}left{frac{m}{n}iright}^2-msum_{i=1}^{n-1}i+nsum_{i=1}^{n-1}left{frac{m}{n}iright}\
          &=frac{m^2}{n}frac{1}{6}(n-1)n(2n-1)-ndsum_{i=0}^{n/d-1}left(frac{d}{n}iright)^2\
          &qquad -mfrac{1}{2}(n-1)n+ndsum_{i=0}^{n/d-1}left(frac{d}{n}iright)tag{7}\
          &=frac{1}{6}m^2(n-1)(2n-1)-frac{d^3}{n}frac{1}{6}left(frac{n}{d}-1right)frac{n}{d}left(frac{2n}{d}-1right)\
          &qquad-frac{1}{2}mn(n-1)+d^2frac{1}{2}left(frac{n}{d}-1right)frac{n}{d}tag{8}\
          &,,color{blue}{=frac{1}{6}left(2m^2n^2-3m^2n+m^2-3mn^2+3mn+n^2-d^2right)}
          end{align*}

          and the claim (1) follows.




          Comment:




          • In (3) we use that positive and negative parts in (2) correspond to each other.


          • In (4) we expand the inner sums.


          • In (5) we rearrange the terms and factor out $leftlfloorfrac{m}{n}irightrfloor$.


          • In (6) we rewrite the expression using the fractional part ${x}=x-lfloor xrfloor$ of $x$.


          • In (7) we expand the sums with linear and quadratic terms and we apply the identity
            begin{align*}
            sum_{i=1}^{n}fleft(left{frac{m}{n}iright}right)=dsum_{i=0}^{n/d-1}fleft(frac{d}{n}iright)
            end{align*}

            where $d=gcd(m,n)$.


          • In (8) we expand the sums and simplify the expression in the final step.








          share|cite|improve this answer














          share|cite|improve this answer



          share|cite|improve this answer








          edited Dec 17 '18 at 7:35

























          answered Dec 16 '18 at 22:31









          Markus ScheuerMarkus Scheuer

          60.4k455144




          60.4k455144























              0












              $begingroup$

              Given $n$ and $mgeq n$ you can determine the numbers
              $$r_{nm}(j):=leftlfloor{m jover n}rightrfloorqquad(1leq jleq n-1)$$in $O(n)$ steps, and another $O(n)$ steps then compute
              $$f(n,m)=sum_{j=1}^{n-1}r_{nm}(j)bigl(2 m j-n(r_{nm}(j)+1)bigr) .tag{1}$$
              One arrives at this formula after observing that by symmetry it is sufficient to consider the lattice points $(j,k)$ below the line $y={mover n}x$. Look at these lattice points on the ordinate $x=j$. Their $y$-coordinates $k$ run through the set $[r_{nm}(j)]$. The"integrand" $p(j,k):=|m j-n k|=mj -n k$ produces an arithmetic progression on these lattice points. Therefore the sum of the $p$-values along the ordinate $x=j$ is their number $r_{nm}(j)$, times the arithmetic mean of the first and the last $p$-values. This leads to formula $(1)$.






              share|cite|improve this answer









              $endgroup$


















                0












                $begingroup$

                Given $n$ and $mgeq n$ you can determine the numbers
                $$r_{nm}(j):=leftlfloor{m jover n}rightrfloorqquad(1leq jleq n-1)$$in $O(n)$ steps, and another $O(n)$ steps then compute
                $$f(n,m)=sum_{j=1}^{n-1}r_{nm}(j)bigl(2 m j-n(r_{nm}(j)+1)bigr) .tag{1}$$
                One arrives at this formula after observing that by symmetry it is sufficient to consider the lattice points $(j,k)$ below the line $y={mover n}x$. Look at these lattice points on the ordinate $x=j$. Their $y$-coordinates $k$ run through the set $[r_{nm}(j)]$. The"integrand" $p(j,k):=|m j-n k|=mj -n k$ produces an arithmetic progression on these lattice points. Therefore the sum of the $p$-values along the ordinate $x=j$ is their number $r_{nm}(j)$, times the arithmetic mean of the first and the last $p$-values. This leads to formula $(1)$.






                share|cite|improve this answer









                $endgroup$
















                  0












                  0








                  0





                  $begingroup$

                  Given $n$ and $mgeq n$ you can determine the numbers
                  $$r_{nm}(j):=leftlfloor{m jover n}rightrfloorqquad(1leq jleq n-1)$$in $O(n)$ steps, and another $O(n)$ steps then compute
                  $$f(n,m)=sum_{j=1}^{n-1}r_{nm}(j)bigl(2 m j-n(r_{nm}(j)+1)bigr) .tag{1}$$
                  One arrives at this formula after observing that by symmetry it is sufficient to consider the lattice points $(j,k)$ below the line $y={mover n}x$. Look at these lattice points on the ordinate $x=j$. Their $y$-coordinates $k$ run through the set $[r_{nm}(j)]$. The"integrand" $p(j,k):=|m j-n k|=mj -n k$ produces an arithmetic progression on these lattice points. Therefore the sum of the $p$-values along the ordinate $x=j$ is their number $r_{nm}(j)$, times the arithmetic mean of the first and the last $p$-values. This leads to formula $(1)$.






                  share|cite|improve this answer









                  $endgroup$



                  Given $n$ and $mgeq n$ you can determine the numbers
                  $$r_{nm}(j):=leftlfloor{m jover n}rightrfloorqquad(1leq jleq n-1)$$in $O(n)$ steps, and another $O(n)$ steps then compute
                  $$f(n,m)=sum_{j=1}^{n-1}r_{nm}(j)bigl(2 m j-n(r_{nm}(j)+1)bigr) .tag{1}$$
                  One arrives at this formula after observing that by symmetry it is sufficient to consider the lattice points $(j,k)$ below the line $y={mover n}x$. Look at these lattice points on the ordinate $x=j$. Their $y$-coordinates $k$ run through the set $[r_{nm}(j)]$. The"integrand" $p(j,k):=|m j-n k|=mj -n k$ produces an arithmetic progression on these lattice points. Therefore the sum of the $p$-values along the ordinate $x=j$ is their number $r_{nm}(j)$, times the arithmetic mean of the first and the last $p$-values. This leads to formula $(1)$.







                  share|cite|improve this answer












                  share|cite|improve this answer



                  share|cite|improve this answer










                  answered Nov 30 '18 at 14:23









                  Christian BlatterChristian Blatter

                  172k7113326




                  172k7113326






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Mathematics Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      Use MathJax to format equations. MathJax reference.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3019794%2fcalculating-sum-limits-i-1n-1-sum-limits-j-1m-1-mi-nj%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Quarter-circle Tiles

                      build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

                      Mont Emei