C、208 206 201 200 199 196 194 193 194 193 188 185 183 180 176 バイト
(改行が削除されている場合):
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);}}
でコンパイル
> gcc -std=c99 -o ulam ulam.c
警告。このプログラムは、2^31 までの除算を試行するので遅いです。しかし、必要な出力が生成されます。
* *
* *
* * *
* * *
* ** *
* *
* *
* *
* *
適切にフォーマットされた 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?' ':'*');
}
}
これは、グリッドの座標を適切な数値に変換してから、蛇のような方法で描画する代わりに、素数テストを実行することによって機能します。4 つの「象限」のさまざまな方程式は、x と y を入れ替えて 1 つにまとめることができ、「後方カウント」の項を追加できます。