Variables

Hi all. I am very very new on that so don’t be mad with my ignorance. I am just trying to create a simple algorithm that can find the Greatest Common Divisor of two numbers and gest what … I can find the way to insert those two numbers. The io. read just don’t work. Any suggestions on that ?

Check the keyboard functions from the doc?

@Thomas

If you get stuck and need an example to look at, here is a simple program.


function setup()
    displayMode(FULLSCREEN)
    showKeyboard()
    fontSize(40)
    fill(255)
    str=""
    nbr=1
    g1=0;g2=0;g3=0
    n1=0;n2=0
    a1=0;a2=0
end

function draw()
    background(30,30,0,255)
    
    if nbr==1 then
        str2="1st"
    elseif nbr==2 then
        str2="2nd"
        a1=n1;a2=0
        n3=0
    end
    
    text(string.format("Enter %s number then press return",str2),WIDTH/2,650)
        
    if n1>0 then
        a1=n1;a2=0
        g3=0
    end
    if n2>0 then
        a2=n2
    end
    
    str3=string.format("GCD (%d,%d) = %d",a1,a2,g3)
    text(str3,WIDTH/2,500)    
    
    if nbr==3 then
        gcd()
        n1=0;n2=0
        nbr=1
    end
end


function keyboard(key)
    if key == RETURN then    -- return key pressed
        str=""
        nbr = nbr + 1
    else
        if key>="0" and key<="9" then
            str=str..key
            if nbr==1 then
                n1=tonumber(str)
            elseif nbr==2 then
                n2=tonumber(str)
            end
        end
    end
end

function gcd()
    g1=n1;g2=n2;g3=n1
    while g1~=g2 do
        g3=math.abs(g1-g2)
        g4=math.min(g1,g2)
        g1=g3;g2=g4
    end
end

…and it works … thanx again