0

I want to know how I can improve my code by helping it find out what number is prime and what is not. I was thinking that I would divide a number by a number and then if it is a decimal number then it is prime, I want it to have a loop to check every number 1 to 100 and see if it is a prime number

This is what I have so far:

for(let i = 1; i <= 100; i++) {
if(i == 1) {
    
}else if(i == 2) {
    console.log(`${i} is a prime number`);
}else if(i >= 3){
    x = i / 2;
    tf = Number.isInteger(x);
    if(tf == false && i >= 3) {
        console.log(`${i} is a prime number`);
    }
}
}

and so far it outputs 1 2 and all the odd numbers.

  • 1
    A number is prime if it is not divisible by any number before it, excluding 1. That's how you check if a number is a prime number. – Taplar Nov 04 '20 at 17:33
  • Just follow the definition of a prime number: any number that can only be divided with itself and 1 is prime. So 2 is prime by default because there is no number between 1 and 2. To check if 3 is prime try dividing it by 2. To check if 4 is prime try dividing it by 2 and 3. To check if 5 is prime try dividing it by 2 and 3 and 4 ... and so on. So to check if 37 is prime try dividing it by 1 and 2 and 3 and 4 .... and 17 and 18 and ... 35 and 36. – slebetman Nov 04 '20 at 17:42
  • 1
    This question has already answers at https://stackoverflow.com/questions/40200089/number-prime-test-in-javascript#:~:text=function%20isPrime(num)%20%7B%20if,log(isPrime(121))%3B and your question does not include any nodejs. – Berke Kaan Cetinkaya Nov 04 '20 at 17:48

3 Answers3

1

Create a function to test whether a number is prime or not (divisible only by 1 and itself). Then call this function inside the loop on each number.

function isPrimeNumber(no) {
  if (no < 2) {
    return false;
  }
  for (let i = 2; i < no; i++) {
    if (no % i == 0) {
      return false;
    }
  }
  return true;
}

for (let i = 1; i <= 100; i++) {
  if (isPrimeNumber(i)) {
    console.log(i);
  }
}
Lahiru Tennakoon
  • 621
  • 1
  • 6
  • 8
0

var numbers = new Array(101).fill(0).map((it, index) => index);
var halfWay = Math.floor(numbers.length / 2);

for (let i = 2; i <= halfWay; i++) {
  if (!numbers[i]) continue;
  
  for (let j = 2; j * i < numbers.length; j++) {
    console.log(`${i} * ${j} = ${i * j}`);
    numbers[j * i] = null;
  }
}

console.log(numbers.filter(it => it));

Here is an attempt to mathematically find numbers between 1-100 that are primes.

  • Fill an array of numbers 0-100
  • For every number (starting at 2), multiply it by itself and all numbers after it, up to half of the array
  • For every computed number, remore it from the array, as it is not a prime
  • At the end, filter out all numbers that are null
Taplar
  • 24,788
  • 4
  • 22
  • 35
0

As Taplar stated primes are numbers that only divide by the number itself and 1.

As far as improving your code. I would say you want to eliminate as many possible numbers with the fewest questions.

An example would be is the number even and not 2 if so it is not prime? The interesting part of this question you eliminate dividing by all even numbers as well. This instantly answers half of all possible numbers and halves the seek time with the ones you need to lookup.

So what would this look like?

function isPrime(num) {
  // Check it the number is 1 or 2
  if (num === 1 || num === 2) {
    return true
  }
  // Check if the number is even
  else if (num % 2 === 0) {
    return false;
  }
  // Look it up
  else {
    // Skip 1 and 2 and start with 3 and skip all even numbers as they have already been checked
    for (let i = 3; i <= num/2; i+=2) {
      // If it divides correctly then it is not Prime
      if (num % i === 0) {
        return false
      }
    }
    // Found no numbers that divide evenly it is Prime
    return true
  }
}

console.log('1:', isPrime(1))
console.log('2:', isPrime(2))
console.log('3:', isPrime(3))
console.log('4:', isPrime(4))
console.log('11:', isPrime(11))
console.log('12:', isPrime(12))
console.log('97:', isPrime(97))
console.log('99:', isPrime(99))
console.log('65727:', isPrime(65727))
console.log('65729:', isPrime(65729))
Michael Warner
  • 3,879
  • 3
  • 21
  • 45