Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prime Numbers JavaScript

Can someone please give me guidance on getting the primenumbers here? This is homework so I don't want the answer but some pointers would be greatly appreciated. It's really annoying me :(

I think I'm close. But this problems I have are number 25 and 35. These are not prime but this function is returning them

var getPrimeNumber = function(n) {
    if(n === 1) return "";
    else if(n == 2) return 2;
    else if(n == 3) return 3;
    else { 
        for(i=Math.floor(Math.sqrt(n)); i>=2; i--){
            //console.log(i);//maybe another var in here? 
            if(n%i !==0 && n%2 !==0 && n%3 !== 0)
                return n; // 25/Math.sqrt(25) will be equal to zero this is what gives me 25 !!!   
        } 
    }
};
like image 470
HattrickNZ Avatar asked Jun 30 '13 10:06

HattrickNZ


People also ask

What is the 10001st prime number JavaScript?

What is the 10,001st prime number? The answer is: 104743.


1 Answers

Here's the fastest way to calculate primes in JavaScript, based on the previous prime value.

function nextPrime(value) {
    if (value > 2) {
        var i, q;
        do {
             i = 3;
             value += 2;
             q = Math.floor(Math.sqrt(value));
             while (i <= q && value % i) {
                 i += 2;
             }
        } while (i <= q);
        return value;
    }
    return value === 2 ? 3 : 2;
}

Test

var value, result = [];
for (var i = 0; i < 10; i++) {
    value = nextPrime(value);
    result.push(value);
}
console.log("Primes:", result);

Output

Primes: [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ]

It is very fast, because:

  • It aligns the loop limit to an integer;
  • It uses a shorter iteration loop, skipping even numbers.

It can give you the first 100,000 primes in about 130ms, or the first 1m primes in about 4 seconds.

2021 Update

In this day and age, we should be using ES6 generators for this:

// generates "n" primes that follow "startPrime";
function* nextPrimes(startPrime, n) {
    let value = startPrime, i = 0;
    while (i++ < n) {
        if (value > 2) {
            let k, q;
            do {
                k = 3;
                value += 2;
                q = Math.floor(Math.sqrt(value));
                while (k <= q && value % k) {
                    k += 2;
                }
            } while (k <= q);
        } else {
            value = value === 2 ? 3 : 2;
        }
        yield value;
    }
}

test:

const result = [...nextPrimes(1, 10)];
//=> [2,  3,  5,  7, 11, 13, 17, 19, 23, 29]

function* nextPrimes(startPrime, n) {
let value = startPrime, i = 0;
while (i++ < n) {
    if (value > 2) {
        let k, q;
        do {
            k = 3;
            value += 2;
            q = Math.floor(Math.sqrt(value));
            while (k <= q && value % k) {
                k += 2;
            }
        } while (k <= q);
    } else {
        value = value === 2 ? 3 : 2;
    }
    yield value;
}
}

const result = [...nextPrimes(1, 10)];

display('Primes: ' + result.join(', '));

function display(msg) {
   document.body.insertAdjacentHTML(
  'beforeend',
  '<p>' + msg + '</p>'
);
}
like image 162
vitaly-t Avatar answered Oct 13 '22 11:10

vitaly-t