Find the least $n$ such that $f(n)/g(n)=4/7$ 
Let $f(n)$ and $g(n)$ be functions satisfying $$f(n)=\cases{\sqrt{n} \hspace{50pt} \sqrt{n} \in \mathbb{N} \\ 1+f(n+1) \hspace{14pt} \text{otherwise}} $$
$$g(n)=\cases{\sqrt{n} \hspace{50pt} \sqrt{n} \in \mathbb{N} \\ 2+g(n+2) \hspace{14pt} \text{otherwise}}$$
for positive integers $n$. Find the least positive value of $n$ such that $\dfrac{f(n)}{g(n)}=\dfrac47$.

I found the values for $f$ and $g$ till $n=9$, I noticed that $f(n)=g(n)$ happened at $n=1,2,4,5,7,9$ and for some values it also happened that if $f(a)=b$, then $f(b)=a$. I don't know if that is useful in anyway. I can't see how to approach the problem.
Please give some hints.
 A: For any number $n$, consider the distance to the next perfect square.  This is either even (including $0$) or odd.  If it's even... that is, if $n=x^2-2k$, with $k<x$... then $f(n)=g(n)=x+2k$.  So for the ratio to be $4/7$ (to be anything other than $1$, really), you must have $$n=x^2-2k+1=(x+1)^2-2(k+x),$$ with $k<x$.  In that case, $f(n)=x+2k-1$, but $g(n)=3x+2k+1$.  Then
$$
\frac{4}{7}=\frac{f(n)}{g(n)}=\frac{x+2k-1}{3x+2k+1}\implies12x+8k+4=7x+14k-7\implies5x=6k-11.
$$
There will be infinitely many solutions to this that satisfy $k<x$; all must have $k\equiv 1$ (mod $5$), so you can just check $k=1,6,11,\ldots$  The smallest is $(x,k)=(17,16)$, followed by $(23,21)$, $(29,26)$, and so on; generally $(x,k)=(17+6q, 16+5q)$ for $q\ge 0$.  These correspond to infinitely many solutions for $n=x^2-2k+1$: $n=258, 488, 790, \ldots$  The general solution is $$n=(17+6q)^2-2(16+5q)+1=258+194q+36q^2.$$
But by far the easiest approach is just to code it up: in Python,
>>> def f(n): return getSqrt(n) or 1 + f(n+1)

>>> def g(n): return getSqrt(n) or 2 + g(n+2)

>>> [n for n in xrange(1,10000) if 7*f(n) == 4*g(n)]
[258, 488, 790, 1164, 1610, 2128, 2718, 3380, 4114, 4920, 5798, 6748, 7770, 8864]

assuming that you've defined getSqrt(n) to return $\sqrt{n}$ when $n$ is a perfect square and otherwise return None.
A: I can not believe the number of arithmetic errors I made.  All inexcusable. but.
Let $n > 0$ so there is a $k$ so that $(k-1)^2 < k \le n^2$ and let $j = k^2 - n$.
Then $$f(n) = f(k^2-j)= 1 + f(k^2-j+1) = 2+f(k^2 - j + 2) =.... =j + f(k^2 -j + j) = j + f(k^2) = j + k$$. (That holds if $j=0$.)
Now similarly if $j$ is even then $g(n) = j +k$ as well.  But if $j$ is odd then.
$$g(k^2 - j) = (j-1) + g(k^2 - 1) = (j+1) + g(k^2 + 1) = (j + 2k + 1) + g(k^2+ 2k + 1) = (j+2k +1) + g((k+1)^2) = (j+2k +1) + (k+1) = j + 3k + 2$$.
So if $\frac {f(n)}{g(n)} \ne 1$ we must have $j$ is odd and  $\frac {f(n)}{g(n)} = \frac {j+k}{j + 3k + 2}$.
So we need $\frac {j+k}{j + 3k + 2} = \frac {4M}{7M}$ and
$j + k = 4M; j + 3k + 2 = 7M$ and $j$ is odd.  And ... arithmetic the bane of mathematicians.
$j= 4M - k$ (so $k$ is odd)
$4M - k + 3k + 2 = 4m + 2k + 2 = 7M$ so $k = \frac {3M-2}2$.
A bit of manipulation we can see than $M$ is divisible by $4$ as $k$ is odd. ($\frac {3M-2}2 = 2w -1\implies 3M=4m$).
Plugging in $M=4w$ we get
$k = \frac {12w-2}2 = 6w -1$ and $j = 16M -k = 10w + 1$.
But we must also have that $j < k^2 - (k+1)^2 = 2k-1$ so $10w + 1 < 12w -3$ so $w > 2$.
Now $w = 3$ and we have $k=17$ and $j = 31$ and $n = 17^2 - 31 > 17^2 -34 +1 = 16^2$.
And $f(17^2 - 31) = 31 + f(17^2) = 48 = 4\cdot 12$.
And $$g(17^2 -31) = 30 + g(17^2 -1)=32 + g(17^2+1)=32+34 + g(17^2 + 34 + 1)=66+g(18^2) = 84 = 7\cdot 12$$.
So $n = 17^2 - 31 = 258$ is the smallest such $n$.
(To minimimize $(k-1)^2 < n=k^2 -j < k^2$ where $n = k^2 -j=(6w-1)^2-(10w + 1)=36w^2 -20w=4w(6w-5)$ we must minimize $w$.)
A: Computing the Functions
Define the "next square root" function
$$
s(n)=\left\lceil\sqrt{n}\right\rceil\tag1
$$
and the "next square root with the same parity" function
$$
p(n)=2\left\lceil\frac{\sqrt{n}-[n\text{ odd}]}2\right\rceil+[n\text{ odd}]\tag2
$$
where $[\cdots]$ are Iverson Brackets.
Then
$$
\begin{align}
f(n)&=s(n)+s(n)^2-n\tag{3a}\\
g(n)&=p(n)+p(n)^2-n\tag{3b}
\end{align}
$$
$\frac{f(n)}{g(n)}=\frac47$ when
$$
n\in\{258,488,790,1164,1610,2128,2718,3380,4114,4920,5798,6748,7770,8864\}
$$
That is all for $n\le10000$.
However, this approach relies on brute force computation to figure out when $\frac{f(n)}{g(n)}=\frac47$.

Mathematica Code
s[n_] := Ceiling[Sqrt[n]]
q[n_] := If[OddQ[n], 1, 0]
p[n_] := 2 Ceiling[(Sqrt[n] - q[n])/2] + q[n]
f[n_] := s[n] + s[n]^2 - n
g[n_] := p[n] + p[n]^2 - n
Flatten@Position[Table[f[n]/g[n], {n, 1, 10000}], 4/7]


A More Understandable Approach
Note that
$$
(s(n)-1)^2\lt n\le s(n)^2\tag4
$$
When $s(n)-n$ is even, the next square root with the same parity is $p(n)=s(n)$ and so $f(n)=g(n)$.
Thus, to get $\frac{f(n)}{g(n)}=\frac47$, we need $s(n)-n$ to be odd. This means that the parity of $n$ and $s(n)$ are different; therefore, the next square root with the same parity is
$$
p(n)=s(n)+1\tag5
$$
Furthermore,
$$
\begin{align}
g(n)&=f(n)+2s(n)+2\tag{6a}\\
\frac34f(n)&=2s(n)+2\tag{6b}\\
3n&=3s(n)^2-5s(n)-8\tag{6c}\\[6pt]
&=(3s(n)-8)(s(n)+1)\tag{6d}
\end{align}
$$
Explanation:
$\text{(6a)}$: apply $(5)$ to $(3)$
$\text{(6b)}$: $g(n)=\frac74f(n)$
$\text{(6c)}$: apply $(3)$
$\text{(6d)}$: factor
$(4)$ and $\text{(6c)}$ give
$$
\overbrace{3s(n)^2-6s(n)+3}^{3(s(n)-1)^2}\lt\overbrace{3s(n)^2-5s(n)-8}^{3n}\tag7
$$
which says we need $s(n)\gt11$.
$\text{(6d)}$ guarantees that $n$ is even since $3s(n)-8$ has the same parity as $s(n)$ and $(s(n)+1)$ has the opposite parity. Since $s(n)-n$ needs to be odd, $s(n)$ needs to be odd.
$\text{(6d)}$ requires that $s(n)\equiv2\pmod3$.
Thus, if $s\gt11$, $s$ is odd, and $s\equiv2\pmod3$, then $n=\frac{(3s-8)(s+1)}3$ will give $\frac{f(n)}{g(n)}=\frac47$.
Therefore, let $s=6k+5$, and then $\frac{f(n)}{g(n)}=\frac47$ precisely when
$$
\bbox[5px,border:2px solid #C0A000]{n=2(k+1)(18k+7)\quad\text{and}\quad k\ge2}\tag8
$$
When $k=2$, we get $n=258$ as the smallest $n$ so that $\frac{f(n)}{g(n)}=\frac47$.
