forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargestPrimeDivisor.java
executable file
·58 lines (55 loc) · 2.01 KB
/
LargestPrimeDivisor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* All integer numbers are divisible by primes. If a number is divisible by more than one prime number,
* then it obviously has a largest prime divisor. The numbers which do not fall in this category do not
* have a largest prime divisor. Given a number N your job is to write a program that finds its largest
* prime divisor. An integer number n is divisible by another integer number m if there is an integer t
* such that mt = n.
* Input
* The input file contains at most 450 sets of inputs. Each line contains a decimal integer N. N does
* not have more than 14 digits. Input is terminated by a line containing a single zero. So no other line
* except the last line contains a zero in the input. This line need not be processed.
* Output
* For each line of the input produce one line of output. This line contains an integer LPD, which is the
* largest prime divisor of the input number N. If the input number is not divisible by more than one
* prime number output a ‘-1’.
* Sample Input
* 2
* 6
* 100
* 0
* Sample Output
* -1
* 3
* 5
*/
//https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2461
import java.util.Scanner;
public class LargestPrimeDivisor {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
long number = input.nextLong();
while (number != 0) {
number = (long) (Math.abs(number));
long largestPrimeDivisor = -1;
int numberOfPrimeDivisors = 0;
int sqrtOfNumber = (int) (Math.sqrt(number));
for (int i = 2; i <= sqrtOfNumber; i++) {
if (number % i == 0) {
numberOfPrimeDivisors++;
largestPrimeDivisor = i;
while (number % i == 0) {
number = number / i;
}
}
}
if (largestPrimeDivisor != -1 && number != 1) {
System.out.println(number);
} else if (numberOfPrimeDivisors <= 1) {
System.out.println(-1);
} else {
System.out.println(largestPrimeDivisor);
}
number = input.nextLong();
}
}
}