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;
	}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s