Pages

Sunday 30 December 2012

UVA - 11466 - Largest Prime Divisor

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        while (true) {
            long x = Math.abs(Long.parseLong(br.readLine()));
            if (x == 0) {
                break;
            }
            long temp = -1;
            long counterP=0;
            long tempX=x;
            for (int i = 2; i < Math.sqrt(x) + 1; i++) {
                while(tempX % i == 0) {
                    if(temp!=i){
                        counterP++;
                    }
                    temp=i;
                    tempX/=i;
                }
            }
            if(x!=tempX &&tempX!=1){
                temp=tempX;
                counterP++;
            }
            if(counterP<2)
                sb.append(-1).append("\n");
            else
                sb.append(temp).append("\n");
        }
        System.out.print(sb);
    }
}

No comments:

Post a Comment