-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution3.java
More file actions
30 lines (26 loc) · 853 Bytes
/
Copy pathSolution3.java
File metadata and controls
30 lines (26 loc) · 853 Bytes
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
public class Solution3{
static long findMaxPrimeFact(long num)
{
long maxPrime = 0;
//First divide out all 2's from the number.
while(num % 2 == 0){
maxPrime = 2;
num = num/2;
}
//Since we divided out all 2's, largest possible factor is now sqrt(n), smallest factor is 3, and we can skip over even factors of n.
for(int i = 3; i < Math.sqrt(num); i+=2){
while(num % i == 0){
maxPrime = i;
num = num / i;
}
}
//case where num itself is prime and greater than 2
if (num > 2)
maxPrime = num;
return maxPrime;
}
public static void main(String []args){
long n = 600851475143L;
System.out.println(findMaxPrimeFact(n));
}
}