Quote by rammer
Suppose we have a spring placed vertically on a table. We put a body with mass "m" on it. The spring compresses by "x".
Then sum of forces is zero, therefore:
kx=mg, so k=mg/x

Here you have
gently lowered the mass onto the spring. Once placed in its new equilibrium position (where the spring is compressed by an amount x = mg/k), it just sits there. Note that your hand does work (negative work) on the mass as you lower it, reducing the total energy of the system.
But if we look on it as the energy problem, then: we hold the body on the string, then we release it, so loss of gravitational potential energy is "mgx" and gain of spring energy is 0.5*k*x^2.
From that:
k=2mg/x, which contradicts previous equation.
Where I did a mistake?

Here you just release the mass. So all the initial gravitational PE remains to be converted to spring PE and kinetic energy, with none being removed by your hand. When it gets to the equilibrium position (at x = mg/k) it still has kinetic energy left, so it keeps going. It reaches the point x
_{2} = 2mg/k, where the KE is momentarily zero, then comes back up. It will continue to oscillate between the two extreme positions.