# 代码高尔夫：圆内的晶格点

15 ``````f = 5  (blue points)
f = 13 (blue + red points)
``````

``````f    = 29
f   = 317
f = 3,141,549
f = 12,566,345
``````

9

## J，21 19 18

``````+/@,@(>:|@j./~@i:)
``````

JB

@JB：是的，嗯...我在偷东西`>:`derp
Jesse Millikan

JB

5

## J，27 21

``````3 :'+/,y>:%:+/~*:i:y'
``````

belisarius博士2011年

1
@belisarius：0.86秒。使用10年的旧硬件。2000
JB

4

## Ruby 1.9， 62 58 54个字符

``````f=->r{1+4*eval((0..r).map{|i|"%d"%(r*r-i*i)**0.5}*?+)}
``````

``````f
=> 3147833

t=Time.now;f;Time.now-t
=> 0.003361411
``````

4

Python 55个字符

``````f=lambda n:1+4*sum(int((n*n-i*i)**.5)for i in range(n))
``````

`f=lambda n:1+4*sum(int((n*n-i*i)**.5)for i in range(n))`短17个字符。
Ventero 2011年

3

``f n=1+4*sum[floor\$sqrt\$n*n-x*x|x<-[0..n]]``

2

``f n|w<-[-n..n]=sum[1|x<-w,y<-w,x*x+y*y<=n*n]``

1
@flawr这些是花样后卫，如果匹配了花样，则将成功，但可以在打高尔夫球时用作较短的出手。看到这个技巧
xnor

1

## JavaScript（ES6），80字节（非竞争，因为ES6太新了）

``````n=>(a=[...Array(n+n+1)].map(_=>i--,i=n)).map(x=>a.map(y=>r+=x*x+y*y<=n*n),r=0)|r
``````

``````n=>[...Array(n+n+1)].map((_,x,a)=>a.map((_,y)=>r+=x*x+(y-=n)*y<=n*n,x-=n),r=0)|r
``````

ES7版本，也是80个字节：

``````n=>[...Array(n+n+1)].map((_,x,a)=>a.map((_,y)=>r+=(x-n)**2+(y-n)**2<=n*n),r=0)|r
``````

1

## Python 2，48个字节

``f=lambda n,i=0:i>n or(n*n-i*i)**.5//1*4+f(n,i+1)``

``f=lambda n,i=0:i>n or 4*int((n*n-i*i)**.5)+f(n,i+1)``

1

# C（gcc），60个字节

``r,a;f(x){for(a=r=x*x;a--;)r-=hypot(a%x+1,a/x)>x;x=4*r+1;}``

``````r,a;
f(x){
for(a=r=x*x;a--;)
r-=hypot(a%x+1,a/x)>x;
x=4*r+1;
}``````

1

# APL（Dyalog扩展），14字节

``{≢⍸⍵≥|⌾⍀⍨⍵…-⍵}``

``````{≢⍸⍵≥|⌾⍀⍨⍵…-⍵}            Monadic function taking an argument n.
⍵…-⍵             n, n-1, ..., -n
⌾⍀                   Make a table of complex numbers
(equivalent to ∘.{⍺+1J×⍵} in Dyalog APL)
⍨                  with both real and imaginary parts from that list.
|                       Take their magnitudes.
⍵≥                        1 where a magnitude are is at most n, and 0 elsewhere.
⍸                           Get all indices of truthy values.
≢                            Find the length of the resulting list.``````

1

# Japt`-x`，12个字节

``````òUn)ï Ëx²§U²
``````

``````òUn)            #Get the range [-input ... input]
ï           #Get each pair of numbers in that range
Ë         #For each pair:
x        # Get the sum...
²       # Of the squares
§      # Check if that sum is less than or equal to...
U²    # The input squared
#Output the number of pairs that passed the check
``````

1

1

# PHP，85 83字节

``````function f(\$n){for(\$x=\$n;\$x;\$c+=\$x,\$y++)for(;\$n*\$n<\$x*\$x+\$y*\$y;\$x--);return\$c*4+1;}
``````

``````f(1001)=3147833
time=0.000236 seconds.
``````

``````/**
* 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;
}
``````

0

# Clojure / ClojureScript，85个字符

``````#(apply + 1(for[m[(inc %)]x(range 1 m)y(range m):when(<=(+(* x x)(* y y))(* % %))]4))
``````

@muddyfish我没有注意到年龄，只是在顶部附近看到了。Clojure早于该问题，但我对它的历史了解不足以了解语言变化。
MattPutnam '16

0

## Mathematica，35个字符

``````f[n_]:=Sum[SquaresR[2,k],{k,0,n^2}]
``````

https://reference.wolfram.com/language/ref/SquaresR.html

`SquaresR[2,k]`是将k表示为两个平方之和的方式的数量，与半径为k ^ 2的圆上的晶格点的数量相同。从k = 0到k = n ^ 2求和，以找到半径为n的圆上或圆内的所有点。

1
`2~SquaresR~k~Sum~{k,0,#^2}&` 使它更短
jaeyong

0

# Tcl，111个字节

``````lassign {1001 0 -1} r R x
while {[incr x]<\$r} {set R [expr {\$R+floor(sqrt(\$r*\$r-\$x*\$x))}]}
puts [expr {4*\$R+1}]
``````

`3147833.0`在约1030微秒内计算f（1001）→ ，AMD Sempron 130 2.6GHz 64位处理器，Windows 7。