简体   繁体   中英

finding prime numbers in an array from 1-200

A math problem describes a list of numbers from 1-200, you must skip the number 1, and then for each number that follows, remove all multiples of that number from the list. Do this until you have reached the end of the list.

Here's what I have so far.

var x = []; // creating an array called x with zero numbers

for ( var i = 1; i <= 200; i++ ){
    x.push(i);
};

// x now should have an array that contains the intergers from 1-200.

//looping through the array.

for ( var i = 0; i <= x.length; i++ ){         //going from 1-200
    if (x[i] == 1){
        continue;  // skipping 1
        } else {
            for ( var n = i+1; n <= i; n++){  // take the number 1 index bigger than x[i] 
                if ( n % i == 0){             //find if the modulus of x[n] and x[i] is zeor, meaning it is divisible by x[i]
                    x.shift();                //remove that number
                    console.log(x[n]);
                } else {
                    continue;
                }

        }
    }

};

Instead of adding number 1 to 200 and then removing non prime numbers, try only putting prime numbers into that list. Since this is a school problem (I'm guessing) I don't want to give you the answer, but if you have more questions I can answer.

Also your nested loop will never run, go over that logic again.

Here is, I believe, a working example of what you want:

function isPrime(num){
  if(num < 2){
    return false;
  }
  for(var i=2,l=Math.sqrt(num); i<=l; i++){
    if(num % i === 0){
      return false;
    }
  }
  return true;
}
function range(f, t){
  for(var i=f,r=[]; i<=t; i++){
    r.push(i);
  }
  return r;
}
function primeRange(from, to){
  var a = range(from, to), r = [];
  for(var i=0,l=a.length; i<l; i++){
    var v = a[i];
    if(isPrime(v)){
      r.push(v);
    }
  }
  return r;
}
var prmRng = primeRange(1, 200);
console.log(prmRng);

Another version (a minute too late, as always ;-), one with comments

// lil' helper
function nextSet(a,n){
  while(a[n] == 0) n++;
  return n;
}

function setPrimes(a,n){
  var k, j, r;
  n = n + 1;
  k = n;
  while(k--)a[k] = 1;
  a[0] = 0; // number 0
  a[1] = 0; // number 1
  // get rid of all even numbers
  for(k = 4; k < n; k += 2) {
    a[k] = 0;
  }
  // we don't need to check all of the numbers
  // because sqrt(x)^2 = x
  r = Math.floor(Math.sqrt(n));
  k = 0;
  while(k < n){
    k = nextSet(a,k+1);
    // a test if we had them all
    if(k > r){
      break;
    }
    // unmark all composites
    for(j = k * k; j < n; j += 2*k){
      a[j] = 0;
    }
  }
  return a;
}

function getPrimes(n){
  // we know the size of the input
  var primearray = new Array(n);
  // we don't know the size of the output
  // prime-counting is still an unsolved problem
  var output = [];

  setPrimes(primearray, n);

  for(var i = 0; i < n; i++){
    if(primearray[i] == 1){
      output.push(i);
    }
  }
  return output;
}
getPrimes(200);

You can go through a full implementation of that algorithm at another primesieve .

I solved it this way:

 let numbers = new Array(); for (let i = 1; i <= 200; i++) { numbers.push(i); } let primeNumbers = (num) => { let prime = new Array(); for(let i = 0; i < num.length; i++) { let count = 0; for (let p = 2; p <= num[i]; p++) { if(num[i] % p === 0 && num[i] !== 2) { count++; } else { if(num[i] === 2 || count === 0 && num[i]-1 === p) { prime[i] = num[i]; } } } } return prime.filter(Boolean); } console.log(primeNumbers(numbers)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM