The formula is, basically:
[(Front teeth + rear teeth)/2] +( length of chainstay in inches between the sprockets)=required links.
(Add a few extra for good luck/just in case)
**EDIT** this is for singlespeed or internal-gear hubs; the method for finding chainlength for derailer systems is, obviously, much different.