ProjectEuler Problem #3 I'm working on projecteuler.net problems, and I made this for problem #3:

array = []; // use this to save the prime factors
function factor(number, start){ //begin of the function
  prime=true;
  for(i = start; i &lt; number+1; i++){ //begin of the first loop
    if(number % i == 0){ //is it a divisor of the number?
       for (x = 2; x &lt;= Math.sqrt(i); x++) { //begin of the second lopp
          if (i % x == 0) {prime = false} //is it a prime number?
        }//end of the second loop
       if(prime){ array.push(i); factor(number/i, i); } //if it is a prime number save it and let's do the proccess again. 
    }
  }  //end of the first loop
} //end of the function
array; //So you can see the result


My problem is:
I can get the prime factors of all the numbers i pass including the one they put on the example(13195), but when i try to get the prime factors of the one in the exercise(600851475143), my code do save the prime factor in the array but then i get the alert of "script not responding" in mozzila and chromes just stay there doing nothing, any way to solve this?.

Ps: i already solved the exercise but im not satisfied and can't fix it by my own.
You have excessive, unnecessary comments.  You wrote your code clean enough that you don't need them.




for (x = 2; x &lt;= Math.sqrt(i); x++) {
  if (i % x == 0) {prime = false}
}



You are wasting your time here.  Once you know the value is not a prime, you do not need to continue looping.  This will reduce your time greatly:

for (x = 2; x &lt;= Math.sqrt(i); x++) {
  if (i % x == 0) {
    prime = false;
    break;
  }
}





if(prime){ array.push(i); factor(number/i, i); }



I'm not sure why you continue finding all the small factors when only the largest prime factors are needed.  In fact, if you are going to do it manually looping over each number, you should start high and work toward the smaller numbers.  A better way to do it, however, would be to use the Sieve of Eratosthenes, as mentioned in the comments on your post.