Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>C, <strike>208</strike> <strike>206</strike> <strike>201</strike> <strike>200</strike> <strike>199</strike> <strike>196</strike> <strike>194</strike> <strike>193</strike> <strike>194</strike> <strike>193</strike> <strike>188</strike> <strike>185</strike> <strike>183</strike> <strike>180</strike> 176 Bytes</h1> <p>(if newlines are removed):</p> <pre><code>main(int u,char**b){ for(int v,x,y,S=v=**++b-48;--v&gt;-S;putchar(10)) for(u=-S;++u&lt;S;){ x=u;y=v;v&gt;-u^v&lt;u?:(x=v,y=u); x=4*y*y-x-y+1+2*(v&lt;u)*(x-y); for(y=1;x%++y;); putchar(y^x?32:42);}} </code></pre> <p>Compiled with</p> <pre><code>&gt; gcc -std=c99 -o ulam ulam.c </code></pre> <p>Warning. This program is slow, because is does a trial division up to 2^31. But is does produce the required output:</p> <pre><code> * * * * * * * * * * * ** * * * * * * * * * </code></pre> <p>In nicely formatted C and with redundant #includes:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; 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&gt;=-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&lt;=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&lt;u; x=u; y=v; if (v&lt;=-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&lt;x &amp;&amp; x%y; ++y) ; putchar(y!=x?' ':'*'); } } </code></pre> <p>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".</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload