Given a positive integer n such that −1 is a quadratic residue mod n, we give an algorithm that computes the integers u and v which satisfy the equation n=u2+v2. To do this, we use the group structure of the Modular group Γ=PSL(2,Z).