<p>Here's some simple but reasonably efficient Python code that does the job.</p>

<pre><code>import math

def T(n):
    "Return sum_{i=1}^n d(i), where d(i) is the number of divisors of i."
    f = int(math.floor(math.sqrt(n)))
    return 2 * sum(n // x for x in range(1, f+1)) - f**2

def count_divisors(a, b):
    "Return sum_{i=a}^b d(i), where d(i) is the number of divisors of i."
    return T(b) - T(a-1)
</code></pre>

<p>Explanation: it's enough to be able to compute the sum from <code>1</code> to <code>b</code>, then we can do two separate computations and subtract to get the sum from <code>a</code> to <code>b</code>.  Finding the sum of the divisor function from <code>1</code> to <code>b</code> amounts to computing <a href="http://oeis.org/A006218" rel="nofollow">sequence A006218</a> from the online encyclopaedia of integer sequences.  That sequence is equivalent to the sum of <code>floor(n / d)</code> as <code>d</code> ranges over all integers from <code>1</code> to <code>n</code>.</p>

<p>And now <em>that</em> sequence can be thought of as the number of integer-valued points under the hyperbola <code>xy=n</code>.  We can use the symmetry of the hyperbola around the line <code>x = y</code>, and count the integer points with <code>x &lt;= sqrt(n)</code> and those with <code>y &lt;= sqrt(n)</code>.  That ends up double counting the points with both <code>x</code> and <code>y</code> less than <code>sqrt(n)</code>, so we subtract the square of <code>floor(sqrt(n))</code> to compensate.  All this is explained (briefly) in the introduction to <a href="http://arxiv.org/abs/1206.3369" rel="nofollow">this paper</a>.</p>

<p>Remarks:</p>

<ul>
<li><p>the algorithm has running time <code>O(sqrt(b))</code>, and constant space requirements.  Improvements in running time are possible at the expense of space; see the paper referred to above.</p></li>
<li><p>for really large <code>n</code>, you'll want a proper integer square root rather than using <code>floor(math.sqrt(n))</code>, to avoid problems with floating-point inaccuracies.  That's not a problem with the sort of <code>n</code> that you're looking at.  With typical IEEE 754 floating-point and a correctly rounded square root operation, you're not going to run into trouble until <code>n</code> exceeds <code>2**52</code>.</p></li>
<li><p>if <code>a</code> and <code>b</code> are <em>really</em> close, there may be more efficient solutions.</p></li>
</ul>
Sum of number of divisor of number between a and b inclusive
<p>Let there be a function <strong>g(x)=number of divisor of x</strong>. Given two integers a and b we need to find-></p>

<p><strong>g(a)+g(a+1)....+g(b).</strong></p>

<p>I thought this step-></p>

<pre><code>for every x from a to b

sum+=number of divisor of x(in sqrt(x) complexity)
</code></pre>

<p>but  its given <strong>1&lt;=a&lt;=b&lt;=2^31-1</strong></p>

<p>So to iterate between a and b can cost me a lot of time....for eg->if a=1 and b=2^31-1.</p>

<p>Is there a better way to do?</p>