CF707C Pythagorean Triples
Solution Sketch
Using Euclid's formula
There are two special cases, $n = 1$ and $n = 2$, where the right triangle can’t be formed (plug in the numbers and you will see why).
After eliminating special cases, we just need to deal with $n$ according to parity.
If $n$ is odd, we can use $\text{equation $(1)$}$ and property
$$
\begin{align}
(k + 1)^2 - k^2 &= 2k + 1 &&\text{the difference between two consecutive numbers’ square is odd}\tag 4
\end{align}
$$
We can get $k = \frac{n - 1}{2}$ (because $2k + 1 = n$), and then substitute $k$ with $\frac{n - 1}{2}$ in $\text{equation $(4)$}$ to obtain $q = \frac{n - 1}{2} + 1$ and $p = \frac{n - 1}{2}$. We can get the answers for odd from $\text{equation $(2)$ and equation $(3)$}$, which are $$2 (\frac{n - 1}{2} + 1) (\frac{n - 1}{2}) \;\text{and}\; (\frac{n - 1}{2} + 1)^2 + (\frac{n - 1}{2})^2$$
If $n$ is even, we can use $\text{equation $(2)$}$ and let $q = 1$ (then $p = \frac n2$). We can the get the answers for even from $\text{equation $(1)$ and equation $(3)$}$, which are $$(\frac{n}{2})^2 - 1 \;\text{and}\; (\frac{n}{2})^2 + 1$$
AC Code
|
|