Code for the dating problem given by John