0
$\begingroup$

How can I find the first perfect square from the function: f(n)=An²+Bn+C? B and C are given. A,B,C and n are always integer numbers, and A is always 1. The problem is to find n.

Example: A=1, B=2182, C=3248

The answer for the first perfect square is n=16, because sqrt(f(16))=196.

My algorithm increments n and tests if the square root is a integer number.

This algorithm is very slow when B or C is large, because it takes n calculations to find the answer.

Is there a faster way to do this calculation? Is there a simple formula that can produce an answer?

  • 0
    Before proceeding with the calculation, you could do a remainder check, using modular arithmetic!2012-02-07
  • 1
    See this [question](http://stackoverflow.com/q/9114874/866022) and [answer](http://stackoverflow.com/a/9115166/866022)2012-02-07

3 Answers 3