function [Q,R] = QR_fact(A)
n = size(A)*[1,0]';
Q = A;
R = eye(n);
for i = 1 : n
    for j = 1 : i-1
        Q(:,i) = Q(:,i) - proj(A(:,i),Q(:,j));
    end
    Q(:,i) = Q(:,i) / sqrt(Q(:,i)'*Q(:,i));
end

for i = 1 : n
    for j = i : n
        R(i,j) = Q(:,i)'*A(:,j);
    end
end
end