title: Nested loops-keep the number of iterations constant
body: <p>By request by @mypetition I am editing my question, although I think that the astronomy details here are unimportant.
  I have a file of the form:</p>
  
  <pre><code>   a          e        q          Q          i        lasc       aper      M          H      dist   comment     i_f      i_h      i_free
  45.23710   0.1394   38.93105   51.54315    5.0300    19.9336   286.2554   164.9683   8.41   51.3773   warm     0.000    62.000     4.796
  46.78620   0.1404   40.21742   53.35498    3.1061   148.9657   192.3009   337.5967   7.37   40.8789   cold     0.000    42.000     2.473
  45.79450   0.1230   40.16178   51.42722    8.0695   104.6470   348.5004    32.9457   8.45   41.3089   warm     0.000    47.000     6.451
  42.95280   0.0145   42.32998   43.57562    2.9273   126.3988   262.8777   163.4198   7.36   43.5518   cold     0.000   161.000     2.186
  </code></pre>
  
  <p>There are 1.6e6 lines in total. These are orbital elements. I need to compute the Minimum Orbit Intersection Distance (MOID) between each pair of orbits, e.g line 1 with line 2, line 1 with line3 and so forth until I reach the end of the file. Then, I start from the second line and go to the end of the file. Then start from the third line and agin go to the end of the file etc. Since I have 1.6e6 orbits, that would be <code>~1e12</code> orbit pairs.</p>
  
  <p>I don't want to load all these 1e12 calculation on 1 cpu and wait forever, so I am planning to use a cluster and launch multiple serial jobs.</p>
  
  <p>I need to iterate over <code>1.6e6</code> elements, where I start with the first elements and go to the end of the file, then start from the second and go to the end of the file etc, until I lastly start with <code>T-1</code> and go to <code>T</code>. These will result in <code>10^12</code> iterations and I am planning split them into multiple jobs, where each job does <code>C=10^7</code> calculations, so I can run them on a computer cluster.<br>
  I came up with the following nested loop:</p>
  
  <pre><code>for i in range( M, N)
    for j in range( i+1, T)
  </code></pre>
  
  <p>where <code>M=1</code> and changes according to the number of jobs that I will have. <code>T=1.6e6</code> is constant (number of lines to iterate over). I want to find the index <code>N</code>, so that the total number of operations is <code>C=10^7</code>. Here is how I approached the problem:
  <code>[T-(N+1) + T-(M+1)]*(M-N+1)/2=C</code>  - because the number of the operations are just the sum of the arithmetic series above. So, I solve the quadratic equation and I get the roots. Here is the python code for that:</p>
  
  <pre><code>import numpy as np
  import math
  
  C=1.0e7  # How many calculations per job do you want?
  T=1.6e6  # How many orbits do you have?
  M=1      # what is the starting index of outer loop?
  #    N = end index of outer loop (this is to be calculated!)
  P=1
  l=0
  with open('indx.txt','w') as f:
  
     while P&lt;T:
        l=l+1
        K=np.roots([-1,2*T,M**2-2*T*(M-1)-2*C])
        N=int(round(K[1]))
  
        f.write("%s %s\n" % (P,P+N))
        M=K[1]+1
        P=P+N+1
  </code></pre>
  
  <p>However, keeping the above solutions, updating M=M+N, I noticed that the condition C=10^7 is not satisfied. Here is a list of the first few indices.</p>
  
  <pre><code>M N
  
  1 7
  8 21
  22 41
  42 67
  68 99
  100 138
  139 183
  184 234
  235 291
   ....
   ....
  1583930 1588385
  1588386 1592847
  1592848 1597316
  1597317 1601791
  </code></pre>
  
  <p>But if you look at the pair before the last, the loop over <code>i=1592848 - 1597316</code> and <code>j=i+1, T</code> will produce more calculations than <code>C=10^7</code> i.e roughly (2685+7153)*4468/2 ~ 2.2e7. </p>
  
  <p>Any idea on how to solve this problem, keeping C=1e7 constant, which will provide the number of jobs (with similar running time) I need to run in order to iterate over 1.6e6 lines. </p>
  
  <p>Hopefully, this explanation is enough according to @mypetition standards and am hoping to resolve the problem.</p>
  
  <p>Your help will be highly appreciated!</p>