$\begingroup$Not sure what temperature and pressure have to do with this problem. How would you expect T and P to restrict formulae?$\endgroup$
– Curt F.Jan 5 at 23:44

$\begingroup$Also, when you say "molecule" I assume you mean "empirical formulae". You're not expecting this algorithm to tell apart glucose (C6H12O6) and galactose (also C6H12O6), right? If what you really want is just empirical formulae, OK. There is no way an algorithm could enumerate all of the possible _structures_/molecules, though.$\endgroup$
– Curt F.Jan 5 at 23:46

1

$\begingroup$As a first answer, ignore valence requirements and just do the combinatorics. If O is in {0, 1} and H is in {0, 1, 2}, then there are six possible formulae, one of which is the nonsense H0O0. I guess that leaves five.$\endgroup$
– Curt F.Jan 5 at 23:57