javascript - Finding the 10001st Prime Number - Project Euler -


i trying find 10,001th prime number. have looked @ other code people have written , don't understand means. have written code in javascript in tried use sieve of eratosthenes. i'm not sure problem is. looks if should work correctly, i'm getting wrong answer.

var compute = function() { var prime = [2,3,5,7,11,13,17,19]; for(var i=20; i<=80000;i++) { if(i%2!==0 && i%3!==0 && i%5!==0 && i%7!==0 && i%11!==0 && i%13!==0 && i%17!==0 &&      i%19!==0) {  prime.push(i);   }  }    console.log(prime[10000]); };   compute(); 

this simple method, find millionth

(or hundred thousandth in machines)

you'll need chop timeouts,

or ship off webworker keep locking up.

you need check prime divisors, collect them,

since every number either product of primes

or prime.

function nthprime(nth){     var p= [2], n= 3, div, i, limit,isprime;     while(p.length<nth){         div= 3, i= 1;         limit= math.sqrt(n)+1;         isprime= true;         while(div<limit){             if(n%div=== 0){                 isprime= false;                 div= limit;             }             else div= p[i++] || div+ 2;         }         if(isprime) p.push(n);         n+= 2;     }     return p[p.length-1]; } 

alert(nthprime(10001));


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -