X=(n/2)(2a+(n-1)d). For an AP, the nth term is given by a+(n-1)d where a is the first term and d the common difference between terms. The sum to n terms=X=is (n/2)(2a+(n-1)d).
So, 2X=2an+n^2d-nd; n^2d+n(2a-d)=2X; n^2+n(2a/d-1)=2X/d;
Completing the square: n^2+n(2a/d-1)+(2a/d-1)^2/4=2X/d+(2a/d-1)^2/4;
(n+(2a/d-1)/2)^2=2X/d+(2a/d-1)^2/4 and n=(1-2a/d)/2±√(8X/d+(2a/d-1)^2)/2.
But n has to be positive, so n=√(8X/d+(2a/d-1)^2/2+(1-2a/d)/2. Applying the quadratic formula gives the same result.