# For what $n$ does there exist $a,b > 0$ such that there are exactly $n$ primes $p$ such that $ap + b$ is a perfect square?

Consider the question: Given positive integers $a,b$, for what primes $p$ is $ap + b$ a perfect square? Denote the solution set by $P(a,b) = \{p \mid p \text{ is prime and } ap + b \text{ is a perfect square}\}$. We then see that $P(a,b)$ can be empty or finite but non-empty, and possibly infinite.

• If $b$ is a quadratic non-residue modulo $a$, then $P(a,b) = \varnothing$. We see this by reducing both sides of $ap + b = q^2$ modulo $a$.
• $P(1,1) = \{3\}$. It is easy to show $3$ is the only prime one less than a perfect square via the factoring $p = (q - 1)(q + 1)$.
• I suspect $P(2,2)$ and many others to be infinite, but I don't know how to prove it. This seems difficult in general, as I'm pretty sure showing $P(1,2)$ to be infinite is an open problem.

I was curious about the case of $|P(a,b)|$ being a positive integer. In particular, I am wondering what the possible values of $|P(a,b)|$ are when we know it to be a positive integer.

Question: For which positive integers $n$ do there exist positive integers $a$ and $b$ such that $|P(a,b)| = n$?

Here is some evidence that there are solutions $(a,b)$ for the first few positive integers:

• For $n = 1$: $P(1,1) = \{3\}$.
• For $n = 2$: $P(5,1) = \{3, 7\}$.
• For $n = 3$: $P(15,4) = \{3, 11, 19\}$.
• For $n = 4$: $P(48,25) = \{2, 3, 7, 17\}$.
• For $n = 5$: $P(168,25) = \{2, 3, 13, 37, 47\}$.
• For $n = 6$: $P(840,169) = \{2, 11, 19, 59, 197, 223\}$ (Peter and I both found this independently).
• For $n = 7$: $P(1680,121) = \{2, 7, 19, 29, 43, 409, 431\}$ (Credit: Peter).
• For $n = 8$: $P(17160,2209) = \{11, 29, 127, 181, 461, 1049, 4243, 4337\}$.
• For $n = 9$: $P(4444440, 529) = \{47, 101, 811, 1021, 22679, 44449, 123449, 277789, 1111087\}$ (Credit: Peter).

I do not write the proofs of these equations, since they all involve $b$ being a perfect square and taking the factoring of a difference of squares. Note that if $b$ is a perfect square, $P(a,b)$ is necessarily finite (but possibly empty) due to this factoring.

As I find likely solutions for greater $n$, I will update this post accordingly.

• I suspect you could find something in the more special case $P(a,1)$. One case will always be $q=2$ which we can rule out for large $a$. Then if you restrict $a$ to prime numbers you only need to consider the case $a=q+1$ and $p=q-1$ so $a,p$ are twin primes and thus $P(a,1)$ will always have only one or two solutions if $a$ has a twin. In general for $P(a,b)$ with $a$ prime in fact you only have at most two solutions so the existence of more requires $a$ to be composite. – Μάρκος Καραμέρης Jun 28 '18 at 22:41
• What kind of answer do you want ? The proof of the conjectures ? Or general considerations ? – Peter Jun 29 '18 at 11:04
• I am not asking for proofs of any conjectures made in the post (I know how to prove the ones at the bottom, I'm just lazy). For now, I'm more interested in answering the primary question. If you have general considerations for that, I'd prefer they be left in the comments if brief enough. – theyaoster Jun 29 '18 at 17:13
• What might be useful : If $a$ and $b$ are positive integers, we can bound the possible primes $p$ , such that $ap+b^2$ is a perfect square as follows : $$ap+b^2=c^2$$ implies $$ap=(c-b)(c+b)$$ Since one of the factors must be coprime to $p$, it must divide $a$, so we have $c-b\le a$ or $c+b\le a$. In both cases we can conclude $c\le a+b$. Hence we have $$ap+b^2\le a^2+2ab+b^2$$ which gives $$p\le a+2b$$ – Peter Jun 30 '18 at 12:24
• Exactly $6$ primes : $(840/169)$ – Peter Jun 30 '18 at 12:50