forked from devForTheFuture/Algorithm-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestPrimeFactor.java
More file actions
32 lines (25 loc) · 791 Bytes
/
LargestPrimeFactor.java
File metadata and controls
32 lines (25 loc) · 791 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
31
32
package algorithms;
/**
* The prime factors of 13195 are 5, 7, 13 and 29.
* What is the largest prime factor of the number 600851475143 ?
*
* @author joeytawadrous
*/
public class LargestPrimeFactor {
public static void main(String[] args) {
long numberToFactor = 600851475143l;
int currentDivisor = 2;
int largestDivisor = 0;
while (numberToFactor != 1) { // cannot divide any further
if(numberToFactor % currentDivisor == 0) { // no remainder
numberToFactor = numberToFactor / currentDivisor;
largestDivisor = currentDivisor;
currentDivisor = 2;
}
else {
currentDivisor++;
}
}
System.out.println("NumberToFactor: " + numberToFactor + " :: CurrentDivisor: " + currentDivisor + " :: LargestDivisor: " + largestDivisor);
}
}