Optimisation Algorithm for a mathematics project