Mathematicians solve a foamy problem