Home > OS >  Finding prime number in javascript
Finding prime number in javascript

Time:08-01

There are several ways of checking if a given number is prime or not. I want to find it in by own way and if the given number is not prime, I am trying to find next higher prime number. so far I tried the following and can't understand why it's creating infinite loop. Could you please correct my code. Or tell me what wrong I am doing here.

function myFunction(a){
 for(i=2; i<=a; i  ){
  if(a % i != 0){
  console.log(" Prime ")
  }
   else{
     a    // creates infinite loop
     if(a % i == 0){
     console.log("not prime")
     } 
     else{
      console.log("prime")
     }   
   }
}
 console.log(a)
}

myFunction(38) // Expected 41

CodePudding user response:

You had several wrong things. If you want a solution see below. If you want hints, I can give you:

  1. two loops or two functions
  2. don't increment the a in the for loop when you are going from i to a
  3. when x % y == 0 then x is not a prime.
  4. you can't stop the loop when x % y != 0 because you haven't proved it is prime yet.

Spoiler:

// code re-use right here
const isPrime = num => {
  for (let i = 2, s = Math.sqrt(num); i <= s; i  )
    if (num % i === 0) return false;
  return num > 1;
}

function closestPrime(num) {
  while (!isPrime(num)) {
    num  
  }
  return num;
}
console.log(closestPrime(38))
console.log(closestPrime(7))

CodePudding user response:

function nextPrime(integer){
  //Store primes and initiate index
  let primes = [2]
  let index = 3
  //while the last prime found is less that the parameter
  while(primes[primes.length -1] <= integer){
    //if index is not divisible by any prime
    if(primes.every(elem => index%elem != 0)){
        //add to primes
        primes.push(index)
    } 
    index   
  }
  //return last prime found (greater than parameter)
  return primes[primes.length -1]
}
console.log(nextPrime(38))

CodePudding user response:

what are mistakes in your code?

  1. what is Prime? prime is a number that divided by all the numbers from 2 to itself - 1, and it is not divided by 1 or itself. so exclude a in for loop, otherwise all numbers are not prime in opinion of your function: for (i = 2; i <= a - 1; i )
  2. Prime numbers follow Counterexample, mean all numbers are prime unless it can divided by a number:
function myFunction(a) {
        for (i = 2; i <= a - 1; i  ) {
            if (a % i == 0) {
                console.log("Not Prime");
                return;
            }
        }
        console.log("Prime");
        return;
    }

3.you have a for loop and many times it need a break or return statement, for example in there. using if you find that the number is not prime, so break or return (if you want to do something for example find next prime, do that and break or return)

  1. at the end of loop and when you find there is no number that can divide a so log this is prime and retuen
  2. do something like that for finding next prime:
function myFunction(a) {
        for (i = 2; i <= a - 1; i  ) {
            if (a % i == 0) {
                console.log("Not Prime");
                while(true) {
                    a  ;
                    let isPrime = true;
                    for (i = 2; i <= a - 1; i  ) {
                        if (a % i == 0) {
                            isPrime = false;
                            break;
                        }
                    }
                    if (isPrime){
                        console.log(a);
                        return;
                    }
                }
            }
        }
        console.log("Prime");
    }

    myFunction(38)
  1. isPrime is a flag that find that if the number is prime or not, if it is true so log and return

at last, a better way:

function isPrime(number) {
    if (number <= 1) {
        return false;
    }
    for (var i = 2; i < number; i  ) {
        if (number % i == 0) {
            return false;
        }
    }
    return true;
}
function myFunction(number) { 
    if (isPrime(number)) {
        return "Prime";
    } else {
        while (true) {
            number  ;
            if (isPrime(number)) {
                return number;
            }
        }
    }
 }
console.log(myFunction(39));
  • Related