A Codility problem: view from mathematics

By

I encountered a simple but interesting problem on codility.com: