The formula comes from the combination of one of the equations of linear acceleration (v=u+at), v =d/t and components of velocity.
The vertical velocity component = vel*sin(ang). Using the equation of linear acceleration above with u=vel*sin(ang), v=-vel*sin(ang) and a=-g and rearranging for t we get the time for the projectile to hit the ground after being fired.
t = 2*vel*sin(ang)/g
Now using v=d/t (constant velocity formula) with the horizontal velocity component: v=vel*cos(ang), d=dist and t is as above we get the following:
dist =2*vel*vel*sin(ang)*cos(ang)/g
vel = sqrt(dist /(sin(2*ang)/g)) using the trigonometric identity that 2*sinx*cosx=sin(2x)
Which is almost what you got... Not sure why we have a square root difference. But that's where the formula is coming from anyways.