Counting unit squares circle passes through Write a program or function that given an integer radius r returns the number of unit squares the circle with radius r centered at the origin passes through. If the circle passes exactly through a point on the grid that does not count as passing through the adjacent unit squares.

Here's an illustration for r = 5:


Illustration by Kival Ngaokrajang, found on OEIS

Examples:


  0 &rightarrow; 0
  1 &rightarrow; 4
  4 &rightarrow; 28
  5 &rightarrow; 28
  49 &rightarrow; 388
  50 &rightarrow; 380
  325 &rightarrow; 2540
  5524 &rightarrow; 44180
  5525 &rightarrow; 44020
Python 2, 54 bytes

f=lambda r,x=0:r-x and-~((r*r-x*x)**.5%1&gt;0)*4+f(r,x+1)

Try it online!
Less golfed (55 bytes) (TIO)
lambda r:8*r-4*sum((r*r-x*x)**.5%1==0for x in range(r))

This estimates the output as 8*r, then corrects for vertex crossings. The result is 8*r-g(r*r), where g(x) counts the number of ways to write x as a sum of two squares (except g(0)=0).
If the circle never went through any vertices, the number of cells touched would equal the number of edges crossed. The circle passes through 2*r vertical gridlines and 2*r horizontal gridlines, passing each one in both directions, for a total of 8*r.
But, each crossing at a vertex counts as two edge crossings while only entering one new cell. So, we compensate by subtracting the number of vertex crossings.
This includes the points on axes like (r,0) as well as Pythagorean triples like (4,3) for r=5.
We count for a single quadrant the points (x,y) with x&gt;=0 and y&gt;0 with x*x+y*y==n, then multiply by 4. We do this by counting the numer of sqrt(r*r-x*x) that are whole number for x in the interval [0,r).