Colors of Pi

Something like this may have been shown before, but doing a forum search didn’t find anything. Using my code to calculate the digits of Pi, I display a different color for each of the digits of Pi. The value of Pi starts at the upper left corner and move across the screen. Tapping the left side of the screen reduces the number of squares across the screen and tapping the right side increases the squares across the screen. You can display the print window to see the calculated value of Pi. I start with squares of 8 pixels wide and 13000 digits of Pi which takes about 6 seconds to calculate. You can modify the square size and digits of Pi. This isn’t very useful, but not much seems to be posted on the forum anyways.

displayMode(FULLSCREEN)

function setup()
    col={color(128,128,255),color(0,0,255),color(0,255,0),color(0,255,255),
            color(255,128,128),color(255,0,255),color(255,255,0),color(255,255,255),
            color(255,0,0),color(0,0,0)}    
    sz=8    -- size of squares
    wd=WIDTH//sz
    calcPi()
end

function draw()
    background(0)
    cnt=0
    for y=1,digits//wd do
        for x=1,wd do
            cnt=cnt+1
            c=tonumber(string.sub(str,cnt,cnt)) -- get digit of pi
            fill(col[c+1])  -- get color from table
            rect(x*sz-sz,HEIGHT-y*sz,sz+1,sz+1) -- draw square
        end
    end
end

function calcPi()
    digits=13000 -- number of digits of Pi
    local pi={}
    local tab={}
    local size=math.ceil(digits/7)+1
    local size1=math.ceil(size*7)
    local sum
    local carry
    local b
    local d1=10000000
    local d2=9999999 
    for z=0,size1 do
        tab[z]=5*z+3
    end
    for t=1,size do
        carry=0
        for z=size1,0,-1 do
            sum=tab[z]*d1+carry
            b=27*z*z+27*z+6
            tab[z]=sum%b
            carry=(sum//b)*(2*z*z-z)
        end       
        tab[0]=sum%d1
        table.insert(pi,sum//d1)
    end
    carry=0
    for z=size,1,-1 do
        b=pi[z]+carry
        if b>d2 then
            b=b-d1
            carry=1
        else
            carry=0
        end
        pi[z]=string.format("%07d",b)
    end
    pi[1]="3"
    str=table.concat(pi)  
    print(str)  
end

function touched(t)
    if t.state==BEGAN then
        if t.x<WIDTH/2 then
            wd=wd-1
            if wd<1 then
                wd=1
            end
        else
            wd=wd+1
            if wd>WIDTH//sz then
                wd=WIDTH//sz
            end
        end
    end
end

I tried the above program with a size of 1 and reading 1,000,000 digits of Pi from a file. It still showed a good distribution of colors. It takes 0.016114 seconds to read 1,000,000 digits of Pi from a txt file.