function [u,k] = GaussSeidel(A,b,u0,prec)

n = length(b);
u = u0;
r = b-A*u;
normr = norm(r);
normr0 = normr;
k = 0;

while normr/normr0 > prec
    for i=1:n
        di = r(i)/A(i,i);
        u(i) = u(i) + di;
        r = r - di*A(:,i);
    end
    normr_prev = normr;
    normr = norm(r);
%     normr/normr_prev, normr/normr0, pause
    k = k+1;
end