If then . For that quadratic equation in k to have an integer solution, the discriminant must be a square. So let , and hence . The solution to that Pell's equation comes from convergents to the continued fraction expansion of . The j'th solution is given by .
The solutions turn out to be sums of consecutive squares, satisfying the equation , where . That raises two questions. (1) Why should be a square? (2) Why is that formula true? I can't answer either of those questions.
The first few values of k, n and p are given in this table (entries taken from the Sloane sequences A001921, A001570 and A001571).