Skip to main content
1 of 5
Peter Taylor
  • 43.1k
  • 4
  • 70
  • 169

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.

Peter Taylor
  • 43.1k
  • 4
  • 70
  • 169