Source: https://www.math.u-bordeaux.fr/IREM/rallye/IMG/pdf/solutions-rallye2013.pdf