Degree of Unsaturation

This is not a particularly hard code puzzle - but I'm interested to see your multiple ways of solving it.

The Degree of Unsaturation is the number of double chemical bonds between atoms, and/or the number rings in a chemical compound.

You will be given the molecular formula of a chemical compound in the form XaYbZc (where a, b and c are the number of atoms of X, Y or Z in the compound) - the formula could be of any length and contain any chemical element in the periodic table (though elements other than C, H, N, F, Cl, Br, I may be ignored as they do not feature in the formula). The compound will contain at least one atom of carbon. You must calculate and display its Degree of Unsaturation.

For example, the compound benzene (pictured below) has a DoU of 4 as it has three double bonds (shown by a double line between atoms), and a single ring (a number of atoms connected in a loop):

benzene ring

As defined by LibreTexts:

DoU = (2C + 2 + N − X − H ) / 2


  • C is the number of carbon atoms
  • N is the number of nitrogen atoms
  • X is the number of halogen atoms (F, Cl, Br, I)
  • H is the number of hydrogen atoms

Test cases:

C6H6 --> 4
C9H2O1 --> 0
C9H9N1O4 --> 6
U1Pt1 --> Not a valid input, no carbon
Na2O1 --> Not a valid input, no carbon
C1H1 --> 1.5, although in practice this would be one, but is a part of a compound rather than a compound in entirety. 
N1H3 would return 0 - though in practice it isn't an organic compound (in other words it contains no carbon) so the formula wouldn't apply and it isn't a valid input

For an explanation of CH see here

In essence, you must identify if there are any of the above elements (C, H, N, F, Cl, Br, I) in the compound, and if so how many there are. Then, calculate the Degree of Unsaturation using the above formula.

Only C, H, N, F, Cl, Br, and I are valid inputs for the DoU formula. For the purposes of this puzzle, any other elements may be completely ignored (eg if the compound were C6H6Mn the result would still be 4). If there are none of the above compounds the answer would be zero.

You may assume that all the compounds input are chemically possible, contain at least one atom of carbon, and are known to exist. If the input is invalid, the program may output either 0 or -1, or produce no result.


Standard IO rules and loopholes apply. Input must be a standard string, and you can assume the input won't be empty. This is codegolf - so the shortest code in bytes wins.

  • \$\begingroup\$ Proposed test cases: Sodium oxide: Na2O and Methylidyne: CH and CCl4He. These are some corner cases that may break a few solutions. By the way, not that it matters for anyone other than Mathematica (probably), but can we assume that the compounds (can) exist? \$\endgroup\$ Jun 27, 2017 at 21:35
  • \$\begingroup\$ I don't understand C9H2O1 --> 0. Shouldn't it be 9? (2*9+2+0-0-2)/2 \$\endgroup\$
    – DLosc
    Jun 28, 2017 at 7:34
  • \$\begingroup\$ according to last paragraph, do you mean the code must be able to deal with invalid inputs? By the way, is it guaranteed that every single element in the compound has a trailing '1' as in C1H1? \$\endgroup\$
    – Keyu Gan
    Jun 28, 2017 at 8:38
  • \$\begingroup\$ @KeyuGan yes and yes. \$\endgroup\$ Jun 28, 2017 at 8:40

4 Answers 4


JavaScript (ES6), 117 112 bytes

Returns 0 for invalid inputs.


Test cases

let f =


console.log(f("C6H6"))     // --> 4
console.log(f("C9H20"))    // --> 0
console.log(f("C9H9N1O4")) // --> 6
console.log(f("U1Pt1"))    // --> 0 (invalid)
console.log(f("Na2O1"))    // --> 0 (invalid)
console.log(f("C1H1"))     // --> 1.5
console.log(f("N1H3"))     // --> 0 (invalid)

Alternate version, 103 bytes

If the input was guaranteed to be valid -- as the challenge introduction is misleadingly suggesting -- we could just do:



let f =


console.log(f("C6H6"))     // --> 4
console.log(f("C9H20"))    // --> 0
console.log(f("C9H9N1O4")) // --> 6
console.log(f("C1H1"))     // --> 1.5


Python 3, 142 151 148 bytes

import re
m=lambda k:int(l.get(k,0))
print(m("C")and m("C")+1+(m("N")-sum(map(m,"F I H Cl Br".split())))/2)

Returns 0 on error.

Thanks to @HyperNeutrino bringing the bytes down.

Try it online!

  • \$\begingroup\$ oops - test cases updated! \$\endgroup\$ Jun 27, 2017 at 22:48
  • \$\begingroup\$ Does't quite work \$\endgroup\$
    – hyper-neutrino
    Jun 27, 2017 at 22:50
  • \$\begingroup\$ @HyperNeutrino Test cases were unclear for a bit. Now has no output on invalid input. \$\endgroup\$ Jun 27, 2017 at 23:18
  • \$\begingroup\$ 148 bytes \$\endgroup\$
    – hyper-neutrino
    Jun 28, 2017 at 0:56
  • \$\begingroup\$ Nice use of dict there! \$\endgroup\$
    – DLosc
    Jun 28, 2017 at 7:41

Pip, 70 67 bytes


Takes the chemical formula as a command-line argument. Outputs 0 for invalid inputs. Try it online!


Uses a series of regex replacements to turn the chemical formula into a mathematical formula, evals it, and makes a couple tweaks to get the final value.

The replacements (slightly ungolfed version):

aR+XDs._R"C ""+2*"R"N "'+R`(H|F|I|Cl|Br) `'-RXU.XX"+0*"

a                    Cmdline arg
 R+XD                 Replace runs of 1 or more digits (\d+)
     s._               with a callback function that prepends a space
                       (putting a space between each element and the following number)
 R"C "                Replace carbon symbol
      "+2*"            with +2* (add 2* the number of carbon atoms to the tally)
 R"N "                Replace nitrogen symbol
      '+               with + (add the number of nitrogen atoms to the tally)
 R`(H|F|I|Cl|Br) `    Replace hydrogen or halogen symbol
                  '-   with - (subtract the number of atoms from the tally)
 RXU.XX               Replace uppercase letter followed by another char ([A-Z].)
       "+0*"           with +0* (cancel out numbers of all other kinds of atoms)

We eval the resulting string with V. This gives us 2C + N − X − H. To get the correct value, we make the following adjustments:


             V...  Value of expression calculated above
          /2*      multiplied by 1/2
        1+         plus 1
`C\d`Na            Is carbon in the original formula? (i.e. C followed by a digit)
       &           Logical AND: if no carbon, return 0, otherwise return the formula value

C (gcc), 195 197 202 bytes

Probably the longest answer.


Try it online!

Returns 0 on error.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.