Editorial for COCI '07 Contest 5 #2 Pascal


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Directly implementing the given algorithm is too slow.

A more efficient approach is to realize what the given program does: it calculates the difference between N and its largest divisor. We can find the largest divisor of N by dividing N by its smallest divisor greater than 1.

If N is prime, then the solution is N-1. Otherwise, its smallest divisor is at most \sqrt N so we can use trial division to find it.


Comments

There are no comments at the moment.