JavaScript (240 202 195 151 characters)
Update: Another much smaller version without function (a lot of credits to @mellamokb):
for(x=3,e=d=f=a>>1,c=2;(x&1?x&2?++e<a-d:--e>d:x&2?++f<a-d-1:--f>d)||++x&3||d--;c
++)for(g=0;g<2*a*a;z[g+=c]=1)z[c]||z.getContext("2d").fillRect(e,f,1,1)
Works with this HTML:
<script>a = 50</script>
<canvas id=z width=50 height=50></canvas>
25x25 example (zoomed in) - 800x800 example
This new version now performs well and outputs the right size (NxN) for any odd a
.
Found some small improvements (195 now). Thanks @mellamokb.
Old version:
c=1;i=e=0;b={};for(d=[];c<a*a;){d.push("");for(i+=e+=2;i--;)d[Math.min(e-2,i)]+=
j();d.unshift("");for(i-=e;++i<e;)d[g=Math.max(0,i)]=j()+d[g]}x.innerHTML=d.join
("\n");function j(){if(f=!b[++c])for(h=c*c;h<2*a*a;h+=c)b[h]=1;return f?".":" "}
Currently takes variable a
as input and outputs to an element with the id x
:
<script>a = 50</script>
<pre id=x>
I used the Sieve of Eratosthenes for prime generation, which works really well. Output is quite slow so far though. Don't expect this to run for huge n yet.
size of the spiral
do you mean the number to go up to, or the number of spirals? \$\endgroup\$