You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0600-0699/0633.Sum of Square Numbers/README_EN.md
+134
Original file line number
Diff line number
Diff line change
@@ -191,4 +191,138 @@ impl Solution {
191
191
192
192
<!-- solution:end -->
193
193
194
+
<!-- solution:start -->
195
+
196
+
### Solution 2: Mathematics
197
+
198
+
This problem is essentially about the conditions under which a number can be expressed as the sum of two squares. This theorem dates back to Fermat and Euler and is a classic result in number theory.
199
+
200
+
Specifically, the theorem can be stated as follows:
201
+
202
+
> A positive integer $n$ can be expressed as the sum of two squares if and only if all prime factors of $n$ of the form $4k + 3$ have even powers.
203
+
204
+
This means that if we decompose $n$ into the product of its prime factors, $n = p_1^{e_1} p_2^{e_2} \cdots p_k^{e_k}$, where $p_i$ are primes and $e_i$ are their corresponding exponents, then $n$ can be expressed as the sum of two squares if and only if all prime factors $p_i$ of the form $4k + 3$ have even exponents $e_i$.
205
+
206
+
More formally, if $p_i$ is a prime of the form $4k + 3$, then for each such $p_i$, $e_i$ must be even.
207
+
208
+
For example:
209
+
210
+
- The number $13$ is a prime and $13 \equiv 1 \pmod{4}$, so it can be expressed as the sum of two squares, i.e., $13 = 2^2 + 3^2$.
211
+
- The number $21$ can be decomposed into $3 \times 7$, where both $3$ and $7$ are prime factors of the form $4k + 3$ and their exponents are $1$ (odd), so $21$ cannot be expressed as the sum of two squares.
212
+
213
+
In summary, this theorem is very important in number theory for determining whether a number can be expressed as the sum of two squares.
214
+
215
+
The time complexity is $O(\sqrt{c})$, where $c$ is the given non-negative integer. The space complexity is $O(1)$.
0 commit comments