Very slow Project Euler Q3 (largest prime factor of a large number) My code returns the correct answer for the example (highest prime of  13195 is 29), but when I test against the number in the question my code basically is too slow for my liking.


  The prime factors of 13195 are 5, 7, 13 and 29.
  
  What is the largest prime factor of the number 600851475143?


Is there anything obviously wrong with my logic or any ways to improve?

(function(){

var theNumber = 600851475143; // testing this number, sub in 13195 to verify 

var getHighestPrimeFactor = function(number){
    var upto = (number / 2) % 2 == 0 ? (number/2) : Math.floor(number/2);
    var factors = [];

    for(var i = upto; i &gt; 1; i--){
        if(number % i == 0){
            factors.push(i);
            if(isPrimeFactor(i) == true){
                console.log("high prime = " + i);
                return i;
            }
        }
    }
} // end getHighestPrimeFactor

var isPrimeFactor = function(number){
    var upto = number / 2;
    var isItPrime = true;
    for(var i = 2; i &lt; upto; i++){
        if(number % i == 0){
            isItPrime = false;
            break;
        }
    }
    return isItPrime;
} // end isPrimeFactor

var x = getHighestPrimeFactor(theNumber);
console.log(x);

})();
Similar questions have been asked before.  You are using the "test the largest candidate factors first" strategy, which does not scale well at all.  You should test the smallest candidate factors first.