title: Remove every (k+1) th remaining element in kth pass of natural numbers
body: <p>In the natural numbers series, we've to remove every 2nd element in the 1st pass. Then in the remaining elements, remove every 3rd element in the second pass. Then at Kth pass, remove every (k+1)th element from the remaining elements.</p>
  
  <p>The series will go like this</p>
  
  <pre><code>1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, ...
  </code></pre>
  
  <p>After 1st pass(after removing every 2nd element),</p>
  
  <pre><code>1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, ...
  </code></pre>
  
  <p>After 2nd pass,(after removing every 3rd element),</p>
  
  <pre><code>1, 3, 7, 9, 13, 15, 19, 21, 25, 27, ...
  </code></pre>
  
  <p>After 3rd pass,(after removing every 4th element),</p>
  
  <pre><code>1, 3, 13, 15, 19, 25, 27, ...
  </code></pre>
  
  <p>So, after infinity pass, it will become</p>
  
  <pre><code>1, 3, 7, 13, 19, 27, 39, 49, 63, 79, ...
  </code></pre>
  
  <p>This series is also called Flavius-Josephus sieve.</p>
  
  <p>The solution for this, to find the 6th element in the series:</p>
  
  <ul>
  <li>do 6^2 = 36</li>
  <li>go down to a multiple of 5 giving 35</li>
  <li>then down to a multiple of 4 = 32</li>
  <li>then down to a multiple of 3 = 30</li>
  <li>then down to a multiple of 2 = 28</li>
  <li>then down to a multiple of 1 = 27</li>
  <li>and so the 6th lucky number is 27.</li>
  </ul>
  
  <p>Though it works, I'm not understanding how the solution works ?</p>
  
  <p>A C program for this is,</p>
  
  <pre><code>int calc(int n)
  {
     if (n == 1) return 1;
     return calc_rec(n*n, n-1);
  }
  
  int calc_rec(int nu, int level)
  {
     int tmp;
     if (level == 1) return (nu-1);
     tmp = nu % level;
     return calc_rec(nu - (tmp ? tmp : level), level-1);
  }
  </code></pre>
  
  <p>The link explaining this <a href="http://oeis.org/A000960" rel="nofollow">http://oeis.org/A000960</a></p>