The formula
With L the length between posts, w the baluster width and g the largest gap, the fewest balusters is (L − g) ÷ (w + g), rounded up. That number of balusters makes one more gap than balusters, so each gap is (L − balusters × w) ÷ (balusters + 1), always at or under g. If that gap lands within 1/8 in (3 mm) of g, the calculator adds one more baluster, so a small setting-out error or a shrinking baluster cannot open a gap past the code. Start one gap from the post and step one gap plus one width each time.