Code-Golf: Lattice Points inside a Circle
J, 21 19 18
+/@,@(>:|@j./~@i:)
Builds complexes from -x-xj to x+xj and takes magnitude.
Edit: With >:
Edit 2: With hook and monadic ~
. Runs a few times slower for some reason, but still 10-ish seconds for f(1000).
J, 27 21
3 :'+/,y>:%:+/~*:i:y'
Very brutal: computes sqrt(x²+y²) over the [-n,n] range and counts items ≤n. Still very acceptable times for 1000.
Edit: i:y
is quite a bit shorter than y-i.>:+:y
. Thanks Jesse Millikan!
Ruby 1.9, 62 58 54 characters
f=->r{1+4*eval((0..r).map{|i|"%d"%(r*r-i*i)**0.5}*?+)}
Example:
f[1001]
=> 3147833
t=Time.now;f[1001];Time.now-t
=> 0.003361411