What molecular formulae correspond to a given mass \(M\)? Here, we address this problem - the compomer problem of mass spectrometry. We describe, implement, and test a straightforward and practical aufbau algorithm which leads to
the molecular formula tree from which all possible solutions may be obtained. The algorithm was inspired from the generating function method used in combinatorics, to count the number of compomers. The molecular formula tree can
be generated in linear time with regards to the number of elements \(N\), and in quadratic time with respect to the mass \(M\); and we demonstrate that the memory requirements do not exceed that of a standard laptop for the formula of
a small molecule, say about 10 elements and a mass of 1000 Da. Nevertheless, we still discuss and demonstrate how a range of heuristics can be used to mitigate the memory requirements.