Let $m$ be an odd positive integer. Show that $m$ can be written as a sum of two squares $m = a^2 + b^2$ with $\gcd(a,b) = 1$ if and only if every prime factor of $m$ is congruent to $1 (\text{mod}~4)$.
$\mathbf{My~Attempts:}$
Notice that if $m$ is an odd prime, then the statement holds by Fermat's theorem on sums of two squares.
So, let $m$ be the composite odd positive integer.
First prove if every prime factor of $m$ is congruent to $1~(\text{mod}\ 4)$ then $m = a^2 + b^2$ with $\gcd(a,b) = 1$.
Assume that every prime factor of $m$ is congruent to $1~(\text{mod}\ 4)$
Let $m = p_1 p_2 \cdots p_n$ be the prime factorization of $m$ and each $p_i$ are odd.
Then, by assumption, each $p_i \equiv 1 ~(\text{mod}~4)$ which by Fermat's theorem on sums of two squares, $p_i = a_i^2 + b_i^2$ for some $a_i, b_i \in \mathbb{N}$.
So, $m = (a_1^2 + b_1^2)(a_2^2 + b_2^2) \cdots (a_n^2 + b_n^2) = [(a_1 a_2 + b_1 b_2)^2 + (b_1 a_2 - a_1 b_2)^2](a_3^2 + b_3^2) \cdots (a_n^2 + b_n^2)$.
Let $x_1 = (a_1 a_2 + b_1 b_2)$ and $y_1 = (b_1 a_2 - a_1 b_2)$.
Then, we have $m = (x_1^2 + y_1^2)(a_3^2 + b_3^2) \cdots (a_n^2 + b_n^2)$.
Now repeat this process $n-2$ times and let each $x_i = (x_{i-1} a_{i+1} + y_{i-1} b_{i+1})$ and let each $y_i = (y_{i-1} a_{i+1} - x_{i-1} b_{i+1})$.
Then, we will have $m = (x_{n-1}^2 + y_{n-1}^2)$ where $x_{n-1} = (x_{n-2} a_n + y_{n-2} b_n)$ and $y_{n-1} = (y_{n-2} a_n - x_{n-2} b_n)$.
Where $x_{n-1}$ and $y_{n-1}$ are both positive integers.
Let $a = x_{n-1}$ and $b = y_{n-1}$.
So, we proved that $m$ can be written as a sum of two squares $m = a^2 + b^2$.
$\mathbf{Problems:}$
Now I stuck on how to prove that $\gcd(a,b) = 1$ in this case !!
Also, I don't know how to prove the inverse of the statement where if $m = a^2 + b^2$ with $\gcd(a,b) = 1$ then every prime factor of $m$ is congruent to $1~(\text{mod}~4)$ !