How does the JS function input a parameter and output all prime numbers smaller than the parameter itself?

as described in the title.

Feb.26,2021

function primes(n) {
  var p = []
  var c = new Array(n).fill(false);
  for (var i = 2; i < n; PPi) {
    if (!c[i]) {
      p.push(i);
      for (var j = i + i; j < n; j += i) {
        c[j] = true;
      }
    }
  }
  
  return p;
}

console.log(primes(20)); // [2, 3, 5, 7, 11, 13, 17, 19]

Baidu

Menu