$\frac{\partial f_i}{x_j}=\frac{\partial f_j}{x_i}\implies(f_1,\ldots,f_n)$ is a gradient I was reading a solution when I came across this statement.

So $$\frac{\partial f_i}{x_j}=\frac{\partial f_j}{x_i}.$$ Then there exists a differentiable function $g$ on $\mathbb{R}^n$ such that $\frac{\partial g}{\partial x_i}=f_i$.

Why is this true?
 A: The following proofs assume 2 variables.
Proof of necessary condition:
If $(f_i, f_j)$ is the gradient of a function $F$, it means that:
$$
\frac{\partial{F}}{\partial{x_i}} = f_i \\
\frac{\partial{F}}{\partial{x_j}} = f_j
$$
Now, if $F$ has continuous second partial derivatives, then according to Clairaut's theorem:
$$
\frac{\partial^2{F}}{\partial{x_i}\partial{x_j}} = \frac{\partial^2{F}}{\partial{x_j}\partial{x_i}}
$$
Therefore:
$$
\frac{\partial{f_i}}{\partial{x_j}} = \frac{\partial{f_j}}{\partial{x_i}}
$$
Proof of sufficient condition:
The function $F$, if it exists, has the property:
$$
\frac{\partial{F}}{\partial{x_i}} = f_i
$$
By integrating with $x_j$ constant:
$$
F = \int_{x_{i_0}}^{x_i} f_i \, dx_i + R(x_j) \tag{1}
$$
Now take partial derivatives of both sides with respect to $x_j$:
$$
\frac{\partial{F}}{\partial{x_j}} = \frac{\partial}{\partial{x_j}}\int_{x_{i_0}}^{x_i} f_i \, dx_i + R'(x_j) = f_j
$$
Using differentiation under integral sign:
$$
\frac{\partial{F}}{\partial{x_j}} = \int_{x_{i_0}}^{x_i} \frac{\partial{f_i}}{\partial{x_j}} \, dx_i + R'(x_j) = f_j
$$
Using the assumption that $\displaystyle \dfrac{\partial f_i}{\partial x_j} = \dfrac{\partial f_j}{\partial x_i}$:
$$
\frac{\partial{F}}{\partial{x_j}} = \int_{x_{i_0}}^{x_i} \frac{\partial{f_j}}{\partial{x_i}} \, dx_i + R'(x_j) = f_j
$$
Which we can write as:
$$
\left. f_j \right|_{x_{i_0}}^{x_i} + R'(x_j) = f_j
$$
Therefore:
$$
R'(x_j) = f_j(x_{i_0}, x_j)
$$
And:
$$
R(x_j) = \int_{x_{j_0}}^{x_j} f_j \, dx_j
$$
Plug in back into (1):
$$
F = \int_{x_{i_0}}^{x_i} f_i \, dx_i + \int_{x_{j_0}}^{x_j} f_j \, dx_j
$$
Therefore, we have shown that $F$ exists.
