Categories: archive |code

Project Euler #7

In JavaScript:

//this takes a long time to run to the 10001, change below for correct answer
(function(){
  var primes = [2], highest = 2;

  //change to primes[10001] and have patience
  while(primes[101] === undefined){
    findNextPrime();
  }

  //similar to euler3
  function findNextPrime(){
    highest += 1;
    var isPrime = determinePrime(highest);
    if (isPrime)
      primes.push(highest);
  }

  function determinePrime(highest){
    for (x in primes){
      if (highest % primes[x] === 0)
        return false;
    }
    return true;
  }

  for (var x in primes){
    document.write(x, " - ", primes[x], "<br />" );
  }
})();

on GitHub