If you liked this problem, here is an NRICH task which challenges you to use similar mathematical ideas.
This problem is taken from the UKMT Mathematical Challenges.