If the optimal speed is terminal velocity, this is the formula you need:
$V_t= \sqrt{\frac{2mg}{\rho A C_d }}$
where
- $V_t$ is terminal velocity,
- $m$ is the mass of the falling object,
- $g$ is the Earth's gravity|acceleration due to gravity,
- $C_d$ is the drag coefficient,
- $\rho$ is the density of the fluid through which the object is falling, and
- $A$ is the projected area of the object.
Air density $\rho$ depends on altitude, so you need a table like the International Standard Atmosphere to calculate Vt versus altitude.
m, Cd and A depend on your rocket.
Here's a sample graph, based on a rocket with 3.66 m diameter, 100 ton weight and a constant Cd of 0.75:

Altitude in m on the X axis, terminal velocity in m/s on the Y axis.
As Russell Borogove pointed out, Cd also changes depending on your speed. Here's a graph that shows how it changes:

Unfortunately this means we have a feedback loop: if you want to travel at Vt, Vt depends on Cd which depends on Vt. So instead of a simple formula, you have to do iterative calculations for each altitude.
But, the solution is even more complicated than this. So far, we've ignored gravity losses and looked only at drag.
"Goddard's Problem" asks basically the same question: which strategy to use to get the maximum altitude out of a fixed amount of rocket fuel. I've found several papers that tackle this problem, all contain solutions far too complicated for the scope of this answer (and for me to understand without spending days on it).
Many of the solutions for Goddard's problem contain simplifications as well (e.g. the assumption you can do instantaneous thrust, i.e. dump X amount of mass all at once, instead of having to run the engines for Y minutes).
And as a final observation, most rockets don't launch in an optimal-velocity profile: every launch I've seen reaches Mach 2 far below 10 km.
This lecture contains a decent overview of a practical approach to launch profiles.