Tuesday, September 9, 2014

Largest Prime Factor Challenge

I was tempted not to post this solution because I am not happy with it at all but it solves the promblem



https://projecteuler.net/problem=3


long testNum = 600851475143l;
int largestFactor = 0;
long loopMax = 17425170l; //largest known prime 
for (int i = 3; i * i <= loopMax; i++) {
    boolean isPrime = true;            
    for (int j = 2; j < i; j++) {
        if (i % j == 0) {
            isPrime = false;
            break;
        }
    }                        
    if (isPrime && testNum % i == 0) {
        System.out.println("prime factor: " + i);
        largestFactor = i;
        loopMax = (testNum / i) + 1;
    }
}
System.out.println("result is: " + largestFactor);


No comments:

Post a Comment