10025 - The ? 1 ? 2 ? ... ? n = k problem


Difficulty : easy

Solution Description :

Ad hoc math problem

1 + 2 + 3 + ..... + n = k
=> n*(n+1)/2 = k
=> n^2 + n - 2k = 0

So, n = {-1 + sqrt(1+8k)}/2

Using this equation you got the root of n

if n*(n+1)/2 equal k then print the root of n
Otherwise increment the root of n by 1 until (root*(root+1)/2 - k) is even

remember for positive of negative input always output is same output.