0

I am writing a function that will return an array with prime numbers.

The function should return an array with n elements. (n is a parameter) But it returns only one element. Why?

My codes:

function findPrimes(n)
{
  var arr = [];
  var currIndex = 0;
  var sqrtNum;
  var ceiledNum;
  var ceiledIndex = 0;
  var currCompose;
  var res;
  for (initNum = 2; arr.length < n; ++initNum)
  {
    sqrtNum = Math.sqrt(initNum);
    ceiledNum = Math.ceil(sqrtNum);
      for (currCompose = 2; currCompose <= ceiledNum; ++currCompose)
       {
         res = initNum % currCompose;
         if (res == 0 && initNum != currCompose)
         {
          break;
         }
         else if (res == 0 && initNum == currCompose)
         {
          arr[currIndex] = initNum;
          ++currIndex;
          break;
         }
         else if (res != 0 && initNum != currCompose)
         {
          continue;
         }
         else
         {
          console.log("Impossible result!");
         }
       }
  }
 return arr;
}

findPrimes(2); //return 2
findPrimes(10); //return 2 too

Jsbin

1
  • One gotcha with the JSBin tool you are using is that it shows [2] as 2, which I find a little bit confusing. Your above code is returning a list with 1 item in it, which is not the correct answer (see solutions below), but at least it is the right datatype. Commented Mar 16, 2015 at 8:27

3 Answers 3

1

You should not be comparing initNum to currCompose. Keep in mind that initNum is the number you are checking (say, 71), and currCompose will be at most ceil(sqrt(initNum)) (say 9), so the two will never be equal.

Also note that it is best to append to the list and verify that no divisors where found only after the inner loop has finished.

This modified version works.

function findPrimes(n)
{
    var arr = [];
    var currIndex = 0;
    var sqrtNum;
    var ceiledNum;
    var ceiledIndex = 0;
    var currCompose;
    var res;
    var initNum;



    for (initNum = 2; arr.length < n; ++initNum)
    {
        sqrtNum = Math.sqrt(initNum);
        ceiledNum = Math.ceil(sqrtNum);
        for (currCompose = 2; currCompose <= ceiledNum; ++currCompose)
        {
            res = initNum % currCompose;
            if (res == 0 && initNum != currCompose)
            {
                break;
            }
        }
        if (currCompose == ceiledNum+1)
        {
            arr[currIndex] = initNum;
            ++currIndex;
        }
    }
    return arr;
}

var primes = findPrimes(6);
document.write(primes);
Sign up to request clarification or add additional context in comments.

Comments

1

correct Line 14 of your code as follows and it works like charm.
for (currCompose = 2; currCompose <= initNum; ++currCompose)

4 Comments

This solution does not make use of the optimization that you only need to check for divisors up to sqrt(initNum).
@GregPrisament What do you mean??
The solution given by @AnilSh checks for divisors in the range 2...initNum. But an efficient implementation should only check for divisors in the range 2...sqrt(initNum). That is to say, you want to keep: for (currCompose = 2; currCompose <= ceiledNum; ++currCompose)
Oh, I agreed that. Using initNum is too broad. Both your answer and @AnilSh answer work, but yours could speed up process.
0
function FindPrime(numbers){
  if(numbers.constructor === Array){
    output = [];
    for (var i = numbers.length - 1; i >= 0; i--) {
      if(isPrime(numbers[i]) == true){
        output.push(numbers[i]);
      };
    };
    return output;
  }


}

function isPrime(numb){
  if (numb % 2 == 0) return false;
  for (var i=3; i<= Math.sqrt(numb); i = i + 2) {
    if (numb % i == 0) {
     return false;
    }
  }
  return true;
}

numbers = [1,2,3,4,5];
test = FindPrime(numbers);
console.log('testing', test);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.