PHP, 85 83 bytes
O código:
function f($n){for($x=$n;$x;$c+=$x,$y++)for(;$n*$n<$x*$x+$y*$y;$x--);return$c*4+1;}
Seu resultado (consulte https://3v4l.org/bC0cY para várias versões do PHP):
f(1001)=3147833
time=0.000236 seconds.
O código não destruído:
/**
* Count all the points having x > 0, y >= 0 (a quarter of the circle)
* then multiply by 4 and add the origin.
*
* Walk the lattice points in zig-zag starting at ($n,0) towards (0,$n), in the
* neighbourhood of the circle. While outside the circle, go left.
* Go one line up and repeat until $x == 0.
* This way it checks about 2*$n points (i.e. its complexity is linear, O(n))
*
* @param int $n
* @return int
*/
function countLatticePoints2($n)
{
$count = 0;
// Start on the topmost right point of the circle ($n,0), go towards the topmost point (0,$n)
// Stop when reach it (but don't count it)
for ($y = 0, $x = $n; $x > 0; $y ++) {
// While outside the circle, go left;
for (; $n * $n < $x * $x + $y * $y; $x --) {
// Nothing here
}
// ($x,$y) is the rightmost lattice point on row $y that is inside the circle
// There are exactly $x lattice points on the row $y that have x > 0
$count += $x;
}
// Four quarters plus the center
return 4 * $count + 1;
}
Uma implementação ingênua que verifica $n*($n+1)
pontos (e roda 1000 mais devagar, mas ainda calcula f(1001)
em menos de 0,5 segundos) e o conjunto de testes (usando os dados de amostra fornecidos na pergunta) podem ser encontrados no github .