%Minimalist elimination routine with back-substitution

function x = gsolve(A,b)
    n=size(A,1);
    count=0;
    for k=1:(n-1)
        for i=(k+1):n
            em=A(i,k)/A(k,k)
            count=count+1;
            for j=(k+1):n
                A(i,j)=A(i,j)-em*A(k,j);
                count=count+1;
            end;
            b(i)=b(i)-em*b(k);
            count=count+1;
        end;
    end;
    b
    A
    x=zeros(n,1);
    x(n)=b(n)/A(n,n);
    count=count+1;
    for i=(n-1):-1:1
        sumAx=0;
        for j=(i+1):n
            sumAx=sumAx+A(i,j)*x(j);
            count=count+1;
        end;
        x(i)=(b(i)-sumAx)/A(i,i);
        count=count+1;
    end;
    count
end
