乌兰螺旋之素

17

目标

``````5 4 3 <-- first row has the primes 5 and 3
6 1 2 <-- second row has the prime 2
7 8 9 <-- third row has the prime 7
``````

Tom Carpenter

5

8

Pyth，20个字节

``````f}TPTsuC+R=hZ_GtyQ]]
``````

说明：

``````f}TPTsuC+R=hZ_GtyQ]]   implicit: Z = 0
tyQ     for H in [0, 1, ..., 2*input-2] do:
_G           reverse the order of the lines
+R=hZ             append Z + 1 at the end of each line,
updating Z each time with the new value Z + 1
C                  update G with the transposed of ^
this gives the Ulam's spiral
s                 combine all lines to a big list of numbers
f                      filter for numbers T, which satisfy:
}TPT                    T appears in the prime-factorization of T
(<==> T is prime)
``````

6

MATLAB，48

``````a=fliplr(spiral(input(''))');disp(a(isprime(a)))
``````

`````` 5
3
2
7
``````

``````97
61
59
37
31
89
67
17
13
5
3
29
19
2
11
53
41
7
71
23
43
47
83
73
79
``````

1

6

CJam，42 33字节

``````Xali(2*{_W%zY@,Y+:Y,>a+}*e_{mp},`
``````

``````Xa    Push initial matrix [1].
li    Get input and convert to int.
(2*   Calculate 2*(n-1), which is the number of rotations and row additions needed.
{     Start rotation loop.
_     Copy current matrix for getting number of rows later.
W%    Reverse the order of the rows...
z     ... and transpose the matrix. The combination produces a 90 degree rotation.
Y     Get next value from variable Y (which is default initialized to 2).
@,    Rotate previous matrix to top, and get number of rows. This is the number
of columns after the 90 degree rotation, meaning that it's the length of
Y+    Add first value to row length to get end value.
:Y    Save it in Y. This will be the first value for next added row.
,     Create list of values up to end value.
>     Slice off values up to start value, leaving only the new values to be added.
a+    Wrap the new row and add it to matrix.
}*    End of rotation loop.
e_    Flatten matrix into list.
{mp}, Filter list for primes.
`     Convert list to string for output.
``````

ETHproductions's October

@ETHproductions这不是等效的，因为它是整数除法。例如，对于输入3，结果需要为4。实际上，考虑到这一点，我相信有一个字节要保存。`(2*`应该是正确的。

5

Mathematica 223

``````r=Range;i=Insert;t=Transpose;s@n_:=#~Select~PrimeQ&/@Nest[With[{d=Length@#,l=#[[-1,-1]]},
Composition[i[#,l+3d+2+r[d+2],-1]&,t@i[t@#,l+2d+1+r[d+1],1]&,i[#,l+d+r[d+1,1,-1],1]&,
t@i[t@#,l+r[d,1,-1],-1] &][#,15]]&,{{1}},(n-1)/2]
``````

`````` s{15]
``````

{{197，193，191}，{139，137}，{199，101，97，181}，{61，59，131}，{103，37，31，89，179}，{149，67， 17，13}，{5、3、29}，{151、19、2、11、53、127}，{107、41、7}，{71、23}，{109、43、47、83， 173}，{73、79}，{113}，{157、163、167}，{211、223}}

`````` %// MatrixForm
``````

4

Mathematica，118个字节

``````f=Permute[Range[#*#],Accumulate@Take[Join[{#*#+1}/2,Flatten@Table[(-1)^j i,{j,#},{i,{-1,#}},{j}]],#*#]]~Select~PrimeQ&
``````

``````{(n*n + 1)/2, +1, -n, -1, -1, +n, +n, +1, +1, +1, -n, -n, -n, ...}
``````

``````In[515]:= f[5]
Out[515]= {17,13,5,3,19,2,11,7,23}
``````

1

的Javascript，516 363 304 276 243 240个字节

``````M=15;
\$=Math;
_=\$.sqrt;
/**
* Return M*i+j (i.e. lineal or vector idx for the matrix) of the Ulam Matrix for the given integer
*
* Each Segment (there are 4 in each round) contains a line of consecutive integers that wraps the
* inner Spiral round. In the foCowing example Segments are: {2,3}, {4,5},
* {6,7}, {8,9}, {a,b,c,d}, {e,f,g,h}, {i,j,k,l}, {m,n,o,p}
*
*    h g f e d
*    i 5 4 3 c
*    j 6 1 2 b
*    k 7 8 9 a
*    l m n o p
*
* @param n integer The integer which position in the Matrix we want.
* @param M integer Matrix Order.
*/
/*
* m: modulus representing step in segment in current spirtal round
* v: Step in current spiral round, i.e. n - (inner spirals greatest num.)
* s: the current Segment one of [1, 2, 3, 4] that represents the current spiral round
* L: Segment Length (Current spiral round Order - 1)
* B: inner Spiral Order, for trib¿vial case 1 it's -1 special case handled differently.
* C: relative line (row or column) corresponding to n in current spiral Round
* R: relative line (column or row) corresponding to n in current spiral Round
* N: Curren (the one that contains n) Spiral (matrix) round Order
* D: Difference between M and the current Spiral round order.
*/

/**
* Runs the loop for every integer between 2 and M*M
* Does not check sanity for M, that should be odd.
*/
r=[];
for (x = 2; x < M * M; x++) {
p=1;
// Is Prime?
for (k = 2; p&&k <= _(x); k++)
if (x % k==0) p=0;
if (p) {
B = \$.floor(_(x - 1));
B=B&1?B:B-1;
N = B + 2;
D = (M - N) / 2;
v = x - B * B;
L = B + 1;
s = \$.ceil(v / L);
m = v % L || L;
C = D + (s < 3 ? N - m : 1 + m);
R = s&2 ? D + 1 : D + N;
w= s&1 ? M * C + R : M * R + C;
// /*uncomment to debug*/ console.log("X:" + x + ": " + ((s&1) ? [C, R].join() : [R, C].join()));
r[w] = x;
}
}
``````

``````for(M=15,\$=Math,_=\$.sqrt,r=[],x=2;x<M*M;x++){for(p=1,k=2;p&&k<=_(x);k++)x%k==0&&(p=0);p&&(B=\$.floor(_(x-1)),B=1&B?B:B-1,N=B+2,D=(M-N)/2,v=x-B*B,L=B+1,s=\$.ceil(v/L),m=v%L||L,C=D+(s<3?N-m:1+m),R=2&s?D+1:D+N,w=1&s?M*C+R:M*R+C,r[w]=x)}alert(r);
``````

，，，，，，，，，，，，，，，，，、、 ，199,101 ,,,,,,,,,,,,,,,,, 181 ,,,,,,,,,,,,, 61,59 ,,,, 131 ,,,,,,,,,,,,,,,,,,,, 31,89,179,149,67,17 ,,, 13 ,,,,,,,,,,, 5,3,29 ,,,,, 151 ,, ，19，，2,11,53,127 ,,,, 107，，41，，7 ,,,,,,,,,,, 71 ,,,,,,,,,, ,,, 109，，43 ,,,,, 47 ,,,, 83，，173 ,,,,, 73 ,,,,,, 79 ,,,,,,,,,,,,,,,, 、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

juanmf

juanmf

juanmf

364-> 240，方法是内联编写fn逻辑并删除未使用的测试。
juanmf 2015年