Something you will cherish :). Please find my suggestion inline with the comments.
With your code:
Time required to calculate in nanoseconds: 3798869988
With mine:
Time required to calculate in nanoseconds: 331338
public class PrimeFactorFinder {
private static final long NUMBER = 600851475143L;
public static void main(String[] args) {
long time = System.nanoTime();
long result = 0;
long number = NUMBER;
for(int i = 2; i <=number; i++) {
if(number % i == 0 ){
// 1. You can decrease the number since you do not need it. For 10, you would want to check upto 5 in optimal scenario.
// 2. Avoiding two divisions with just one unlike yours.
number = (long)number/i;
if(isPrime(number)) {
result = number;
break;
}
}
}
System.out.println("Result: " + result + "\nTime required to calculate in nanoseconds: " + (System.nanoTime() - time));
}
private static boolean isPrime(long l) {
// Each prime number can be expressed as 6x+1 or 6x-1 except 2 and 3. Eliminate them.
if(l%2==0 || l%3==0){
return false;
}
// Best way to shorten the search is to navigate upto the sqrt of the number.
long sqrt = (long)Math.sqrt(l);
// That's right. We are incrementing the loop by 6 with 2 and 3 eliminated.
for(long num = 6 ; num<=sqrt; num+=6) {
if(l % (num-1) == 0 || (l%(num+1)==0)) { //Possible primes: 6x+1 and 6x-1
return false;
}
}
return true;
}
}