Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your pseudo-code, Inner most loop, <code>k &lt;- j to i+j</code> can be written as <code>k &lt;- 0 to i</code> (<em>this is by removing <code>j</code></em>). Hence your code can be simplified as follows: </p> <pre><code>r &lt;- 0 for i &lt;- 1 to n do for j &lt;- 1 to i do for k &lt;- 0 to i do // notice here `j` removed r &lt;- r + 1 return r </code></pre> <p>Based on this pseudo-code, I have written a C program(as below) to generate sequence for N = 1 to 10. (you originally tagged question as <a href="/questions/tagged/java" class="post-tag" title="show questions tagged &#39;java&#39;" rel="tag">java</a> but I am writing <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a> code because what you wants is independent of language constraints)</p> <pre><code>#include&lt;stdio.h&gt; int main(){ int i =0, k =0, j =0, n =0; int N =0; int r =0; N =10; for (n=1; n &lt;= N; n++){ // unindented code here r =0; for (i=1; i&lt;=n; i++) for (j=1; j&lt;=i; j++) for (k=0; k&lt;=i; k++) r++; printf("\n N=%d result = %d",n, r); } printf("\n"); } </code></pre> <p>Output of this program is something like:</p> <pre><code> $ ./a.out N=1 result = 2 N=2 result = 8 N=3 result = 20 N=4 result = 40 N=5 result = 70 N=6 result = 112 N=7 result = 168 N=8 result = 240 N=9 result = 330 N=10 result = 440 </code></pre> <p>Then, Tried to explore, <strong>How does it work?</strong> with some diagrams: </p> <p>Execution Tree For <code>N=1</code>: </p> <pre><code>1&lt;=i&lt;=1, (i=1) | 1&lt;=j&lt;=i, (j=1) / \ 0&lt;=k&lt;=i, (K=0) (K=1) | | r=0 r++ r++ =&gt; r = 2 ( 1 + 1 ) </code></pre> <p>That is <code>(1*2) = 2</code> </p> <p>Tree For <code>N=2</code>:</p> <pre><code>1&lt;=i&lt;=2, (i=1)-----------------------(i=2) | |---------|------| 1&lt;=j&lt;=i, (j=1) (j=1) (j=2) / \ / | \ / | \ 0&lt;=k&lt;=i, (K=0) (K=1) (K=0)(K=1)(k=2) (K=0)(K=1)(k=2) | | | | | | | | r=0 r++ r++ r++ r++ r++ r++ r++ r++ =&gt; 8 -------------- --------------------------------- ( 1 + 1) ( 3 + 3 ) </code></pre> <p>That is <code>(1 + 1) + (3 + 3) = 8</code></p> <p>Similarly I drawn a tree for <code>N=3</code>:</p> <pre><code>1&lt;=i&lt;=3, (i=1)-----------------------(i=2)--------------------------------------------(i=3) | |---------|------| |----------------------|----------------------| 1&lt;=j&lt;=3, (j=1) (j=1) (j=2) ( j=1 ) ( j=2 ) ( j=3 ) / \ / | \ / | \ / | | \ / | | \ / | | \ 0&lt;=k&lt;=i, (K=0) (K=1) (K=0)(K=1)(k=2) (K=0)(K=1)(k=2) / | | \ / | | \ / | | \ | | | | | | | | (K=0)(K=1)(k=2)(k=3) (K=0)(K=1)(k=2)(k=3) (K=0)(K=1)(k=2)(k=3) r=0 r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ </code></pre> <p>That is <code>(1 + 1) + (3 + 3) + (4 + 4+ 4)= 20</code></p> <pre><code>N = 1, (1 + 1) = 2 N = 2, (1 + 1) + (3 + 3) = 8 N = 3, (1 + 1) + (3 + 3) + (4 + 4 + 4)= 20 N = 4, (1 + 1) + (3 + 3) + (4 + 4 + 4) + (5 + 5 + 5 + 5) = 40 N = 5, (1 + 1) + (3 + 3) + (4 + 4 + 4) + (5 + 5 + 5 + 5) + (6 + 6 + 6 + 6 + 6) = 70 N = 6, (1 + 1) + (3 + 3) + (4 + 4 + 4) + (5 + 5 + 5 + 5) + (6 + 6 + 6 + 6 + 6) + (7 + 7 + 7 + 7 + 7 + 7)= 112 </code></pre> <p>For N=6 we can also be write above sequence as:</p> <pre><code>(1*2) + (2*3) + (3*4) + (4*5) + (5*6) + (6*7) </code></pre> <p>Finally, I could understand that sum of <code>N</code> in three loop is:</p> <pre><code>(1*2) + (2*3) + (3*4) + (4*5) + (5*6) + ... + (N * (N+1)) </code></pre> <p>With help from math.stackexchange.com, I could simplify this equation:<br> I asked here: <a href="https://math.stackexchange.com/questions/416322/how-to-simplify-summation-equation-in-terms-of-n">How to simplify summation equation in terms of N?</a> </p> <blockquote> <p><img src="https://i.stack.imgur.com/CdzaI.png" alt="image"> </p> </blockquote> <p>So as I commented to your question, Result in term of N is <code>( ((N) * (N+1) * (N+2)) / 3 )</code>.<br> And, I think its correct. I cross checked it as follows:</p> <pre><code>N = 1, (1 * 2 * 3)/3 = 2 N = 2, (2 * 3 * 4)/3 = 8 N = 3, (3 * 4 * 5)/3 = 20 N = 4, (4 * 5 * 6)/3 = 40 N = 5, (5 * 6 * 7)/3 = 70 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload