Most pipefitter's books list the required lengths. IPT's "Pipe Trades Handbook" has some good tables. Unless there's some special design consideration, why re-invent the wheel?
If the pipe assembly falls under the ASME B31.1 Power Piping Code, a specific length isn't given, but here's what it says, in part:
" 108.5 U.S. Customary Bolting"
108.5.1 General
(A) Bolts, bolt studs, nuts, and washers shall comply with applicable standards and specifications listed in Table 126.1 and Table 112. Bolts and bolt studs shall extend completely through the nuts.
(C) Nuts shall be provided in accordance with the requirements of the specification for bolts and bolt studs.