Prime numbers in a spiral

I didn’t have anything better to do, so I thought I would code this Ulam spiral. It’s the prime numbers plotted in red in a counter-clockwise spiral. The starting point (1) is in blue in the center of the screen. It may take awhile to create the 23500 limit prime number table. I have little lines showing the direction if you want to follow the spiral around. You have to turn the iPad in landscape for the correct orientation.


Example of the starting counter clockwise spiral.

    16  15  14 13
     5   4   3 12
     6   1   2 11
     7   8   9 10


supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
    limit=23500
    tm=15            
    prime={}
    for z=1,limit do
        table.insert(prime,z)
    end
    for z=2,math.sqrt(limit) do
        for x=z+1,limit do
            if prime[x]%z==0 then
                prime[x]=0
            end
        end        
    end  
    tabx={1,0,-1,0}
    taby={0,1,0,-1}
end

function draw()
    background(0)
    fill(255)
    count=1
    pos=1
    x=math.floor(WIDTH/2)
    y=math.floor(HEIGHT/2)
    value=1
    for z=1,#prime do
        for b=1,2 do
            fill(255)
            for a=1,count do
                if value>#prime then
                    break
                end
                if value<limit then
                    noStroke()
                    fill(0)
                    if prime[value]~=0 then
                        fill(255,0,0)
                    else
                        fill(255)
                    end
                    if value==1 then
                        fill(0,0,255)
                    end
                    ellipse(x,y,tm)
                end
                x=x+tabx[pos]*tm
                y=y+taby[pos]*tm
                if value>1 then
                    stroke(103, 122, 30, 255)
                    strokeWidth(2)
                    line(x,y,hx,hy)
                end
                hx=x
                hy=y
                value=value+1 
            end
            add()
        end
        count=count+1   
    end
end

function add()
    pos=pos+1
    if pos>4 then
        pos=1
    end
end