Optimal Diameter
The sharpest image is obtained at the optimal diameter for a given focal length. This is due to a trade off between flare and fuzziness. As hole size is increased the picture becomes fuzzier. As hole size is decreased the picture becomes sharper up to a point where flare becomes a problem. There are many opinions on the exact formula used to determine this, however, most are derived from the equation:
optimal diameter = square root (wavelength * focal length * k) * 2
k is set between 0.5 and 1

This calculator uses the following derivation:
optimal diameter = square root(.00007 * focal length)