UVa 11466 – Largest Prime Divisor

#include <iostream>
using namespace std;

int main() {
	long long n,ans,i;
	int counter;
	cin>>n;
	while (n!=0){

		if (n<0)
			n=-n;

		ans = -1;
		counter = 0;
		//print -1 for numbers that has 1 prime divisors as primes, pow of primes (4,8,9)
		for ( i = 2; i*i <= n && n!=1; i++) {
			while (n%i == 0){
				n/=i;
				ans = i;
			}
			if (ans == i)counter++;
		}


		if (n!=1 && ans!=-1)
			ans = n;
		else if (counter==1)
			ans = -1;

		cout<<ans<<endl;

		cin>>n;
	}

}