代码高尔夫:乌拉姆螺旋
挑战
按字符数输出最短的代码 Ulam 螺旋,螺旋大小由用户输入指定。
乌拉姆螺旋是映射素数的一种方法。螺旋从位于中心的数字 1 开始(1 不是素数)并在其周围生成螺旋,将所有素数标记为字符“*
”。非素数将被打印为空格“”。
替代文本 http://liranuna.com/junk/ulam.gif
测试用例
Input:
2
Output:
* *
*
*
Input:
3
Output:
* *
* *
* **
*
*
Input:
5
Output:
* *
* *
* * *
* * *
* ** *
* *
* *
* *
* *
代码计数包括输入/输出(即完整程序)。
The Challenge
The shortest code by character count to output Ulam's spiral with a spiral size given by user input.
Ulam's spiral is one method to map prime numbers. The spiral starts from the number 1 being in the center (1 is not a prime) and generating a spiral around it, marking all prime numbers as the character '*
'. A non prime will be printed as a space ''.
alt text http://liranuna.com/junk/ulam.gif
Test cases
Input:
2
Output:
* *
*
*
Input:
3
Output:
* *
* *
* **
*
*
Input:
5
Output:
* *
* *
* * *
* * *
* ** *
* *
* *
* *
* *
Code count includes input/output (i.e full program).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
Python - 203 个字符
工作原理
这个想法是用需要打印为 '*' 的 x,y 坐标填充 A
该算法从与 2 对应的单元格开始,因此避免了测试 1 素数的特殊情况。
x,y 是感兴趣的单元格
j,k 跟踪我们是否需要 inc 或 dec x 或 y 才能到达下一个单元格
s 是下一个角点 i 的值
t 跟踪 s
all(i%d for d in R(2,i)) 的增量进行素性检查
最后一行相当笨拙。它迭代所有单元格并决定是否放置空格或星号
Python - 203 Characters
How it works
The idea is to fill A with x,y coords that need to be printed as '*'
The algorithm starts at the cell corresponding to 2, so the special case of testing 1 for primality is avoided.
x,y is the cell of interest
j,k keep track of whether we need to inc or dec x or y to get to the next cell
s is the value of i at the next corner
t keeps track of the increment to s
all(i%d for d in R(2,i)) does the primality check
The last line is rather clumsy. It iterates over all the cells and decides whether to place a space or an asterisk
MATLAB:
182167156 个字符脚本
ulam.m
:格式更好一点:
测试用例:
MATLAB:
182167156 charactersScript
ulam.m
:And formatted a little nicer:
Test cases:
Golfscript - 92 个字符
~.(:S+,:R{S\-:|;R{S-:$|>' *'1/[|$.|]2/@:d|~)$< !^=~:$;:y.*4*$-y-)2d*$y-*+:$,{)$\%!},,2==}%n}%
97 个字符
~.(:S+,:R{S\-:|;R{S-:$|>' *'1/[|$.|]2/@:d|~)${$\%!},!=}%n}%
99人物
~.(:S+,{S-}%:R{~):|;R{:$|>' *'1/[|$.|]2/@:d|~)${$\%!},!=}%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~)${$\%!},!=}%n}%
Golfscript - 92 Characters
~.(:S+,:R{S\-:|;R{S-:$|>' *'1/[|$.|]2/@:d|~)$<!^=~:$;:y.*4*$-y-)2d*$y-*+:$,{)$\%!},,2==}%n}%
97 characters
~.(:S+,:R{S\-:|;R{S-:$|>' *'1/[|$.|]2/@:d|~)$<!^=~:$;:y.*4*$-y-)2d*$y-*+.1=3*+:$,2>{$\%!},!=}%n}%
99 characters
~.(:S+,{S-}%:R{~):|;R{:$|>' *'1/[|$.|]2/@:d|~)$<!^=~:$;:y.*4*$-y-)2d*$y-*+.1=3*+:$,2>{$\%!},!=}%n}%
100 characters
~:S.(+,{S(-}%:R{~):|;R{:$|>' *'1/[|$.|]2/@:d|~)$<!^=~:$;:y.*4*$-y-)2d*$y-*+.1=3*+:$,2>{$\%!},!=}%n}%
101 characters
~:S.(+,{S(-}%:R{~):v;R{:$v>:d;' *'1/[v$.v]2/v~)$<!d^=~:$;:y.*4*$-y-)2d*$y-*+.1=3*+:$,2>{$\%!},!=}%n}%
C、 <罢工>208 <罢工>206 <罢工>201 <罢工>200 <罢工>199 <罢工>196 <罢工>194 <罢工>193 <罢工>194 <罢工>193 <罢工>188 <罢工>185 <罢工>183
180176 字节(如果删除换行符):
编译时有
警告。这个程序很慢,因为它试除最多 2^31。但它确实产生了所需的输出:
在格式良好的 C 中并带有冗余的 #includes:
它的工作原理是将网格的坐标转换为适当的数字,然后执行素性测试,而不是以蛇状方式绘制。通过交换 x 和 y 以及“向后计数”的附加项,可以将四个“象限”的不同方程合并为一个方程。
C,
208206201200199196194193194193188185183180176 Bytes(if newlines are removed):
Compiled with
Warning. This program is slow, because is does a trial division up to 2^31. But is does produce the required output:
In nicely formatted C and with redundant #includes:
It works by transforming the coordinates of the grid to the appropriate number and then performing the primality test, intead of drawing in a snake-like manner. The different equations for the four "quadrants" can be collapsed into one with swapping x and y and an additional term for "backward counting".
Ruby 1.8.7,194 个字符
出于某种原因,ruby1.9 需要在第 4 行添加另一个空格:
Ruby 1.8.7, 194 chars
For some reason, ruby1.9 wants another space on line 4:
Python - 171
drhirsch 的 C 移植到 python。
Python - 171
drhirsch's C ported to python.
MATLAB,56 个字符,
基于 @gnovice 解决方案,通过使用 MATLAB 的 螺旋函数:)
测试用例:
MATLAB, 56 characters
based on @gnovice solution, improved by using MATLAB's spiral function :)
Test Cases:
J解决方案:
197 173 165161字节(到目前为止)这不使用OP评论中提到的方法
J solution:
197 173 165161 bytes (so far)this does not use the method mentioned in the comments to the OP
我的第一个代码高尔夫!
红宝石,
309301283271265 个字符My first code golf!
Ruby,
309301283271265 charactersPython 2.x,
220C213C207C204C201C198C< /del>196C188C特别感谢 gnibbler 在
#stackoverflow< 中提供一些提示/code> Freenode。输出包括前导和尾随换行符。
(Python 3 兼容性需要额外的字符;这使用
input
< /a>,print
语句和/
用于整数除法。)Python 2.x,
220C213C207C204C201C198C196C188CSpecial thanks to gnibbler for some hints in
#stackoverflow
on Freenode. Output includes a leading and trailing newline.(Python 3 compatibility would require extra chars; this uses
input
, theprint
statement and/
for integer division.)Ruby - 158 个字符
与这个相同的算法,只是素数测试不同
Ruby - 158 Characters
Same algorithm as this one, just the prime test is different
Haskell - 224 个字符
我不是最擅长 Haskell 所以这里可能会出现更多的收缩
echo 6 | 的输出runghc ulam.hs
这是一个不同的算法(类似于@drhirsch的)不幸的是我似乎无法得到低于239个字符
Haskell - 224 characters
i'm not the best at haskell so there is probably some more shrinkage that can occur here
output from
echo 6 | runghc ulam.hs
this is a different algorithm (similar to @drhirsch's) unfortunately i cannot seem to get it below 239 characters
第一篇文章!(哦等等,这不是SlashDot?)
我的团队参赛作品Clojure,
685528 个字符。First post! (oh wait, this isn't SlashDot?)
My entry for Team Clojure,
685528 characters.不像之前的 C 条目那么漂亮,但这是我的。
注意:我发帖是因为它采用了与前一个不同的方法,主要
它适用于输入 > > 9(两位数 - 没有
-47
技巧),在不可读的 C 语言中充分翻译后,变成了 339 个字符。
编译:
gcc -o ulam_compr ulam_compr.c
适用于 osxi686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465)
和 debian Lenny。
这是一些输出:
Not as beautiful as the previous C entry, but here's mine.
note: I'm posting because it takes a different approach than the previous one, mainly
it works with input > 9 (two digits - no
-47
trick)which, adequately translated in unreadable C, becomes 339 chars.
compile with:
gcc -o ulam_compr ulam_compr.c
works on osxi686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465)
and debian Lenny.
Here is some output:
Python - 176
这个以一长串换行符开始,并替换除行尾所需的换行符之外的所有换行符。
从中心开始,算法每一步都会观察左角。如果那里有换行符,则向左转,否则继续前进。
Python - 177
使用字符串可以避免“连接”,但由于字符串是不可变的,因此会多出一个字节
Python - 176
This one starts with a big long list of newline characters and replaces all of them except for the ones that are needed at the end of the lines.
Starting at the centre, the algorithm peeps around the lefthand corner at each step. If there is a newline character there, turn left otherwise keep going forward.
Python - 177
Using a string avoids "join" but ends up one byte longer since the string is immutable
Python,299 个字符:
Python, 299 characters:
Lua,302 个字符
lua ulam.lua 6 的输出:
Lua, 302 characters
Output from
lua ulam.lua 6
:Python
284 266 256 243 242240 char我想尝试递归,我确信它可能会被大大缩短:
根据评论中的建议进行编辑
Python
284 266 256 243 242240 charI wanted to try recursion, I'm sure it may be heavily shortened:
edited under suggestion in comments
Mathematica 243
用法
13 个绕组:
Mathematica 243
Usage
13 windings: