Python (59 bytes)
lambda n:(n<1)+4*sum((-(n%(2*x+1)<1))**x for x in range(n))
Online demo to follow when ideone.com comes back online
As with my CJam answer, this uses Möbius inversion and runs in pseudoquasilinear time.
lambda n:(n<1)+4*sum((-(n%(2*x+1)<1))**x for x in range(n))
Online demo to follow when ideone.com comes back online
As with my CJam answer, this uses Möbius inversion and runs in pseudoquasilinear time.