Finding $r$ in quadratic integer equation $$M=f(n)=\frac{n(n+1)}{2}$$
$$M=g(k)=40k+30+r$$
I have found that $r=2,6$ but $r$ can only be one. Since $n, k \in \mathbb{N}$, I wanted to see which vaue for r would lead to integer solutions for k and n.
$$n^2 +n -2(40k+30+r)=0$$
$$n=\frac{-1+\sqrt{1-4(1)(-1)(80k+60+2r)}}{2}$$
$$n= \frac{-1+\sqrt{320k +241+8r}}{2}$$
$$n=\frac{-1+\sqrt{320k +257}}{2},\frac{-1+\sqrt{320k+289}}{2}$$
now $n$ is only an integer if $320k +257$ or $320k + 289$ are perfect squares.
I don't want to add any more context.
$\color{blue}{\mbox{Sufficiency}}$
So we must look at when $320k+257$ and $320k+289$ , separately, are perfect squares. Indeed, note that if these are perfect squares then they are odd hence perfect squares of odd numbers . In particular, we have that the numerator of the expression for $n$ is an even number so $n$ is an integer provided they are perfect squares.
$\color{green}{\mathrm{320k+257}}$
The idea behind showing that no number of the form $320k+257$ is a square, is to show that no number of a simpler form is a square. Let me show you.

Every number of the form $320k+257$ is of the form $5l+2$ for some $l$.

This is easy, since $320k+257 = 5(64k) + 55 + 2 = 5(64k+11)+2$ so we can take $l = 64k+11$.

No number of the form $5k+2$ can be a perfect square.

Suppose that $x^2 = 5k+2$. Let $5r$ be the smallest multiple of $5$ such that $5r&lt;x$. Then $0&lt;x-5r &lt; 5$. We have $$(x-5r)^2 = x^2-10xr+25r^2 = 5(5r^2-2xr +k) + 2$$
So $(x-5r)$ has the same property as $x$ in terms of having a square $2$ more than a multiple of $5$, which means that some number out of $0,1,2,3,4$ must have a square of the form $5k+2$. But this never happens, as can be easily checked!
This shows that no number of the form $5k+2$ is a perfect square, and hence that no number of the form $320k+257$ is a perfect square.
$\color{fuchsia}{\mathrm{320k+289}}$
Using the logic we used previously works somewhat : if $x^2 = 320k+289$ then $(x-320r)$ also has the same property for any $r$. In particular, having all the solutions from $0,1,2,...,319$ whose squares leave a remainder of $289$, will give us the set of all $x$ for which $x^2$ leaves a remainder of $289$ upon division by $320$.
Why? For example, take $k=0$, so $320k+289 = 289 = 17^2$. Then $x = 17$. Now take any number of the form $17+320l$, and square it, it will also leave a remainder $289$ upon division by $320$. For example, $320+17, 17-320,17-10*320$ would all have this property.
Which means, all we need are the numbers from $0,1,...,319$ whose squares leave a remainder $289$ upon division by $320$. I won't explain how one can simplify this (which one can) but the solutions are $17,47,113,143,177$.
That is, let $S = \{17,47,113,143,177\}$ then every $k$ such that $320k+289$ is a perfect square, is of the form $\left\{\frac{(s+320l)^2 + 289}{320} : s \in S, l \in \mathbb Z\right\}$.
$\color{red}{Further \ reading}$
You may look up the notion of quadratic congruences, which has a large theory dedicated to it. In particular, the Jacobi symbol may be used in some instances to decide if squares of the type $ak+b$ exist, and we can use prime factorizations of $a$ to obtain solutions to the problem or provide bounds on the number of solutions.
Quadratic congruences, incidentally, are motivated by the kind of problem you consider i.e. when is a certain kind of quadratic form the square of another number.
$\color{blue}{\mbox{Sufficiency}}$
So we must look at when $320k+257$ and $320k+289$ , separately, are perfect squares. Indeed, note that if these are perfect squares then they are odd hence perfect squares of odd numbers . In particular, we have that the numerator of the expression for $n$ is an even number so $n$ is an integer provided they are perfect squares.
$\color{green}{\mathrm{320k+257}}$
The idea behind showing that no number of the form $320k+257$ is a square, is to show that no number of a simpler form is a square. Let me show you.

Every number of the form $320k+257$ is of the form $5l+2$ for some $l$.

This is easy, since $320k+257 = 5(64k) + 55 + 2 = 5(64k+11)+2$ so we can take $l = 64k+11$.

No number of the form $5k+2$ can be a perfect square.

Suppose that $x^2 = 5k+2$. Let $5r$ be the smallest multiple of $5$ such that $5r&lt;x$. Then $0&lt;x-5r &lt; 5$. We have $$(x-5r)^2 = x^2-10xr+25r^2 = 5(5r^2-2xr +k) + 2$$
So $(x-5r)$ has the same property as $x$ in terms of having a square $2$ more than a multiple of $5$, which means that some number out of $0,1,2,3,4$ must have a square of the form $5k+2$. But this never happens, as can be easily checked!
This shows that no number of the form $5k+2$ is a perfect square, and hence that no number of the form $320k+257$ is a perfect square.
$\color{fuchsia}{\mathrm{320k+289}}$
Using the logic we used previously works somewhat : if $x^2 = 320k+289$ then $(x-320r)$ also has the same property for any $r$. In particular, having all the solutions from $0,1,2,...,319$ whose squares leave a remainder of $289$, will give us the set of all $x$ for which $x^2$ leaves a remainder of $289$ upon division by $320$.
Why? For example, take $k=0$, so $320k+289 = 289 = 17^2$. Then $x = 17$. Now take any number of the form $17+320l$, and square it, it will also leave a remainder $289$ upon division by $320$. For example, $320+17, 17-320,17-10*320$ would all have this property.
Which means, all we need are the numbers from $0,1,...,319$ whose squares leave a remainder $289$ upon division by $320$. I won't explain how one can simplify this (which one can) but the solutions are $17,47,113,143,177$.
That is, let $S = \{17,47,113,143,177\}$ then every $k$ such that $320k+289$ is a perfect square, is of the form $\left\{\frac{(s+320l)^2 + 289}{320} : s \in S, l \in \mathbb Z\right\}$.
$\color{red}{Further \ reading}$
You may look up the notion of quadratic congruences, which has a large theory dedicated to it. In particular, the Jacobi symbol may be used in some instances to decide if squares of the type $ak+b$ exist, and we can use prime factorizations of $a$ to obtain solutions to the problem or provide bounds on the number of solutions.
Quadratic congruences, incidentally, are motivated by the kind of problem you consider i.e. when is a certain kind of quadratic form the square of another number.