Lir*_*una 27 language-agnostic code-golf rosetta-stone
按字符计算的最短代码输出Ulam的螺旋,螺旋尺寸由用户输入给出.
Ulam的螺旋是一种映射素数的方法.螺旋线从中间的数字1开始(1不是素数)并在其周围产生螺旋线,将所有素数标记为字符' *
'.非素数将被打印为空格' '.
替代文字http://liranuna.com/junk/ulam.gif
Input:
2
Output:
* *
*
*
Input:
3
Output:
* *
* *
* **
*
*
Input:
5
Output:
* *
* *
* * *
* * *
* ** *
* *
* *
* *
* *
Run Code Online (Sandbox Code Playgroud)
代码计数包括输入/输出(即完整程序).
Joh*_*ooy 28
_________________________________________________________
/x=input();y=x-1;w=x+y;A=[];R=range;k,j,s,t=R(4) \
| for i in R(2,w*w): |
| A+=[(x,y)]*all(i%d for d in R(2,i)) |
| if i==s:j,k,s,t=k,-j,s+t/2,t+1 |
| x+=j;y+=k |
| for y in R(w):print"".join(" *"[(x,y)in A]for x in R(w)) |
\_________________________________________________________/
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
x=input();y=x-1;w=x+y
A=[];R=range;k,j,s,t=R(4)
for i in R(2,w*w):
A+=[(x,y)]*all(i%d for d in R(2,i))
if i==s:j,k=k,-j;s,t=s+t/2,t+1
x+=j;y+=k
for y in R(w):print"".join(" *"[(x,y)in A]for x in R(w))
Run Code Online (Sandbox Code Playgroud)
它
是如何工作的想法是用需要打印为'*'
的x,y坐标填充A.算法从对应于2的单元格开始,因此避免了测试1的原始性的特殊情况.
x,y是感兴趣的单元格
j,k跟踪我们是否需要inc或dec x或y到达下一个单元格
s是下一个角落的i的值
t跟踪增量到s
all(i%d代表R(2,i)中的d)进行素数检查
最后一行相当笨拙.它遍历所有单元格并决定是放置空格还是星号
gno*_*ice 10
MATLAB: 182 167 156个字符
脚本ulam.m
:
A=1;b=ones(1,4);for i=0:(input('')-2),c=b(4);b=b+i*8+(2:2:8);A=[b(2):-1:b(1);(b(2)+1:b(3)-1)' A (b(1)-1:-1:c+1)';b(3):b(4)];end;disp(char(isprime(A)*10+32))
Run Code Online (Sandbox Code Playgroud)
并格式化更好一点:
A = 1;
b = ones(1,4);
for i = 0:(input('')-2),
c = b(4);
b = b+i*8+(2:2:8);
A = [b(2):-1:b(1); (b(2)+1:b(3)-1)' A (b(1)-1:-1:c+1)'; b(3):b(4)];
end;
disp(char(isprime(A)*10+32))
Run Code Online (Sandbox Code Playgroud)
测试用例:
>> ulam
2
* *
*
*
>> ulam
3
* *
* *
* **
*
*
>> ulam
5
* *
* *
* * *
* * *
* ** *
* *
* *
* *
* *
Run Code Online (Sandbox Code Playgroud)
〜.(:S +,:R {S\ - :|; R {S - :$ |>'*'1/[| $.|] 2/@:d |〜)$ <!^ =〜:$ ;:Y*4*$ - Y-)2D*$ y的 - *+:$ {!)$ \%} ,, 2 ==}%N}%
97个字符
〜.(:S +,:R {S\ - :|; R {S - :$ |>'*'1/[| $.|] 2/@:d |〜)$ <!^ =〜 :!$ ;: Y*4*$ - Y-)2D*$ y的-*+ 1 = 3*+:$,2> {!$ \%},=}%N}%
99个字符
〜.(:S +,{S - }%:R {〜):|; R {:$ |>'*'1/[| $.|] 2/@:d |〜)$ <!^ =〜:!$ ;: Y*4*$ - Y-)2D*$ y的-*+ 1 = 3*+:$,2> {!$ \%},=}%N}%
100个字符
〜:S.(+,{S( - }%:R {〜):|; R {:$ |>'*'1/[| $.|] 2/@:d |〜)$ < !^ =〜:!$ ;: Y*4*$ - Y-)2D*$ y的-*+ 1 = 3*+:$,2> {!$ \%},=}%N}%
101个字符
〜:S.(+,{S( - }%:R {〜):v; R {:$ v>:d;'*'1/[v $ .v] 2/v~)$ < !d ^ =〜:!$ ;: Y*4*$ - Y-)2D*$ y的-*+ 1 = 3*+:$,2> {!$ \%},=}%N} %
(如果删除换行符):
main(int u,char**b){
for(int v,x,y,S=v=**++b-48;--v>-S;putchar(10))
for(u=-S;++u<S;){
x=u;y=v;v>-u^v<u?:(x=v,y=u);
x=4*y*y-x-y+1+2*(v<u)*(x-y);
for(y=1;x%++y;);
putchar(y^x?32:42);}}
Run Code Online (Sandbox Code Playgroud)
编译
> gcc -std=c99 -o ulam ulam.c
Run Code Online (Sandbox Code Playgroud)
警告.这个程序很慢,因为试验分区最多可达2 ^ 31.但是确实产生了所需的输出:
* *
* *
* * *
* * *
* ** *
* *
* *
* *
* *
Run Code Online (Sandbox Code Playgroud)
格式良好的C和冗余#includes:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int u,v,x,y,d,S = atoi(argv[1]);
/* v is the y coordinate of grid */
for (v=S; v>=-S; --v)
/* u is the x coordinate. The second operand (!putchar...) of the boolean or
* is only ececuted a a end of a x line and it prints a newline (10) */
for (u=-S; u<=S || !putchar(10); ++u) {
/* x,y are u,v after "normalizing" the coordintes to quadrant 0
normalizing is done with the two comparisions, swapping and and
an additional term later */
d = v<u;
x=u;
y=v;
if (v<=-u ^ d) {
x=v;
y=u;
}
/* reuse x, x is now the number at grid (u,v) */
x = 4*y*y -x-y+1 +2*d*(x-y);
/* primality test, y resused as loop variable, won't win a speed contest */
for (y=2; y<x && x%y; ++y)
;
putchar(y!=x?' ':'*');
}
}
Run Code Online (Sandbox Code Playgroud)
它的工作原理是将网格的坐标转换为适当的数字,然后执行素性测试,以类似蛇的方式绘制.四个"象限"的不同方程可以折叠成一个交换x和y,另一个术语用于"反向计数".
n=2*gets.to_i-1
r=n**2
l,c=[nil]*r,r/2
r.times{|i|l[c]=i+1;c=i==0||l[c-n]&&!l[c+1]?c+1:l[c-1]&&!l[c-n]?c-n:l[c+n]?c-1:c+n}
r.times{|i|print"1"*l[i]!~/^1?$|^(11+?)\1+$/?'*':' ',i%n==n-1?"\n":''}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,ruby1.9需要第4行的另一个空格:
r.times{|i|l[c]=i+1;c=i==0||l[c-n]&&!l[c+1]?c+1:l[c-1]&&!l[c-n]?c-n :l[c+n]?c-1:c+n}
Run Code Online (Sandbox Code Playgroud)
drhirsch的C移植到python.
S=input();R=range(-S+1,S)
for w in R:
p="";v=-w
for u in R:d=v<u;x,y=[(u,v),(v,u)][(w>=u)^d];x=4*y*y-x-y+1+2*d*(x-y);p+=" *"[(x>1)*all(x%f for f in range(2,x))]
print p
Run Code Online (Sandbox Code Playgroud)
echo 20 |python ulam.py * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
基于@gnovice解决方案,通过使用MATLAB的螺旋函数改进:)
disp(char(isprime(flipud(spiral(2*input('')-1)))*10+32))
Run Code Online (Sandbox Code Playgroud)
测试用例:
>> disp(char(isprime(flipud(spiral(2*input('')-1)))*10+32))
2
* *
*
*
>> disp(char(isprime(flipud(spiral(2*input('')-1)))*10+32))
3
* *
* *
* **
*
*
>> disp(char(isprime(flipud(spiral(2*input('')-1)))*10+32))
5
* *
* *
* * *
* * *
* ** *
* *
* *
* *
* *
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4226 次 |
最近记录: |