How This KPrime Solution works ? Can somebody explain?

https://www.codechef.com/viewsolution/39083607

please can somebody tell the how this solution works ?

this is the solution of Contest Page | CodeChef