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.
3Doesn't Ulam's spiral usually start with 1 in the middle and spiral outward, instead of spiraling inward? – PhiNotPi – 2012-01-08T21:32:44.277
Yeah, it's late so my brain is out of order. – Hauleth – 2012-01-08T22:17:00.880
By
size of the spiraldo you mean the number to go up to, or the number of spirals? – Gareth – 2012-01-08T22:24:13.833Also, are there limits on the number that may be input or is it 1 to infinity? – Gareth – 2012-01-08T22:29:20.357
Square NxN and limit to 100000 – Hauleth – 2012-01-08T22:50:41.013
Also similar to this: http://stackoverflow.com/questions/1805796/code-golf-ulam-spiral
– gnibbler – 2012-01-09T23:02:58.803Wow, the J paper linked to in the original post of that question is amazing. 72 characters!
' .'{~1 p:>:|.(,~$/:@(+/\)@(_1&|.@((}:@(2:#>:@i.))#(<:@+:$1:,],_1:,-))))– Gareth – 2012-01-10T00:31:24.900