1

I intend to find the largest prime factor of a number n in Javascript. However, i think this code is to slow or could be sortened, but i dont see how. The point would be to display the last element of the arrayfactors, which would be the largest factor. Anyone have any tips on how I could shorten this? The problem is that my browser notifies me the page is taking too long to respond for a 12 digit number. Should I perhaps not use an array?

function biggest(n) {
// Makes a list of primes smaller than n
var primes = [2];
for (i=3; i < n ; i++) {
    var j=0;
    while (j<primes.length) 
    {   var quotient = i/primes[j];
    if (quotient !== Math.floor(quotient)) 
            {var hasDivisor = false;
            j++;
            }

        else
            {var hasDivisor = true;
            j=primes.length+1;
            }

    }

    if (hasDivisor == false) 
        {primes.push(i);}
} 


// Makes a list of factors
var factors = [];
for (i=0; i<primes.length; i++) {
    var quotient = n/primes[i];

    if (quotient==Math.floor(quotient)) {

        factors.push(primes[i]);}
}
    if (factors.length==0) {
    factors.push(1);
}


    items.push("De biggest factor is "+ factors[factors.length-1] +"!"); 



}
steedsnisps
  • 125
  • 1
  • 4
  • 1
    You only need to loop until n / 2, and you can use i+2 instead of i++ to only loop over the odd numbers. – Romain Braun Feb 05 '15 at 18:07
  • Thanks for the i+=2 tip, loop up to n/2 where? the first piece of code is to find primes, not necessarily primes dividing n. – steedsnisps Feb 05 '15 at 18:23

1 Answers1

2

You only need a list of primes up to the square root of n, because if n = p * q, one or the other of p or q must be less than the square root of n (except when n is a perfect square). And instead of computing the primes by iterating over all possible divisors, it is much faster to use the Sieve of Eratosthenes; in pseudocode:

function primes(n)
    ps := []
    sieve := makeArray(2..n, True)
    for p from 2 to n
        when sieve[p]
            append p to ps
            for i from p*p to n step p
                sieve[i] := False
    return ps
user448810
  • 17,381
  • 4
  • 34
  • 59
  • Ah yes, don't know why i didn't think about the square root. However is this sieve possible in Javascript? – steedsnisps Feb 06 '15 at 20:47
  • @Wowlolbrommer: Yes, of course the sieve is possible in Javascript. But I'll leave it to you to translate. – user448810 Feb 07 '15 at 00:36
  • do two dimensional arrays even exist in javascript? – steedsnisps Feb 07 '15 at 23:52
  • The `sieve` array is one-dimensional: the indices run from 2 to n, and each item in the array is initialized to `True`. – user448810 Feb 08 '15 at 16:21
  • isn't that a second value added to each item, so a new dimension? – steedsnisps Feb 08 '15 at 18:23
  • The `ps` variable is a linked list. Variable `p` runs from 2 to `n`. When it finds a `True` value, indicating a prime number, it appends the new prime `p` to the list of primes `ps`, then sieves the multiples of the new prime, setting each to `False`, starting from the square of the new prime and ending at `n`. Both the linked list and the `sieve` array are one-dimensional. – user448810 Feb 08 '15 at 20:29