Counting integer points inside a sphere of radius R and dimension D
<p>I am trying to write an efficient algorithm that counts the number of points inside a Sphere of Radius R and Dimension D. The sphere is always at the origin. Suppose we have a sphere of dimension 2 (circle) with radius 5. </p>

<p>My strategy is to generate all possible points within the first quadrant, so for the above example we know that (1,2) is in the circle, so must all + / -  combinations of that point which is simply dimension squared. So for each point found in a single quadrant of an n-dimensional sphere we add 2 ^ dimension to the total count.</p>

<p>I'm not sure if there is a much more efficient solution to this problem but this is what I have so far in terms of implementation.</p>

<pre><code>int count_lattice_points(const double radius, const int dimension) {
int R = static_cast&lt;int&gt;(radius);

int count  = 0;

std::vector&lt;int&gt; points;
std::vector&lt;int&gt; point;

for(int i = 0; i &lt;= R; i++)
    points.push_back(i);

do {
    for(int i = 0; i &lt; dimension - 1; i++)
        point.push_back(points.at(i));

    if(isPointWithinSphere(point, radius)) count += std::pow(2,dimension);
    point.clear();

}while(std::next_permutation(points.begin(), points.end()));

return count + 3;
}
</code></pre>

<p>What can I fix or improve in this situation ?</p>