-2

I have been testing a prime number generator from a formula I created over two decades ago. It's working, somewhat. However, as I'm testing the numbers it generates, my primeNum function returns true for multiples of 5 that my generator is creating.

let five = 5
let fifteen = 15

const primeNum = (num) => num < 2 ? false : ((num === 2) || num % 2 !== 0) ? true : false

const f = primeNum(f)
console.log(f) // true

const ft = primeNum(ft)
console.log(ft) // true

// same with 25, 35, 45, etc.. 

EDIT / UPDATE: I ended up using Prime factorization and closest divisor.

// Prime factorization
const factors = num => {
  let tmp = num
  let result = {};
  for (let i = 2; i <= num; i++) {
    while (num % i === 0) {
      result[i] = (result[i] || 0) + 1;
      num /= i;
    }
  }

  if(shift(tmp, 2) === tmp || shift(tmp, 3) === tmp) {
    return false
  }
  else if(Object.keys(result).length === 1 && tmp % 3 !== 0) {
    return true
  }
}

// Closest divisor
function shift(number, divisor){
  return number + (divisor - (number % divisor)) % divisor
}
Dshiz
  • 3,099
  • 3
  • 26
  • 53
  • 3
    because `5 % 2 !== 0` – Wyck Jul 19 '22 at 20:26
  • Testing whether a number is prime is complex - I don't see how your function can test in any way. – A Haworth Jul 19 '22 at 20:54
  • I got the answer from another stackoverflow answer. Seems like it works okay up to the first handful of primes. – Dshiz Jul 19 '22 at 21:08
  • Does this answer your question? [Prime Numbers JavaScript](https://stackoverflow.com/questions/17389350/prime-numbers-javascript) – Heretic Monkey Jul 22 '22 at 16:08
  • Or [JavaScript: Check if number is prime with recursion](https://stackoverflow.com/q/63083882/215552), [Number prime test in JavaScript](https://stackoverflow.com/q/40200089/215552), [Javascript prime number check](https://stackoverflow.com/q/38643817/215552), [Prime Number Determination Javascript](https://stackoverflow.com/q/15837655/215552)... There are a few questions about prime number checks on Stack Overflow that are easy to find with simple searching... – Heretic Monkey Jul 22 '22 at 16:13
  • Thanks @HereticMonkey. I did simple searching on SO, but none of the answers I found solved my problem. See the answer I posted. That *does* solve my problem and is highly performant. – Dshiz Jul 22 '22 at 16:14
  • ... and is exactly the same as [an answer provided to the duplicate](https://stackoverflow.com/a/41496946/215552). – Heretic Monkey Jul 22 '22 at 16:23
  • I must have missed it. We are human. – Dshiz Jul 22 '22 at 16:27

1 Answers1

0

I was able to solve this thanks to this article

I created a prime generator that I have tested up to 10,000,000 cycles, which produces 2150415026051 as its highest prime. The following returns true for that number in 7.848ms.

const checkPrime = num => {
  if(num === 2 || num === 3){
    return true
  }
  if(num <= 1 || num % 2 === 0 || num % 3 === 0){
    return false
  }
  for(let i = 5; i * i <= num; i += 6){
    if(num % i === 0 || num % (i + 2) === 0){
      return false
    }
  }
  return true
}
Dshiz
  • 3,099
  • 3
  • 26
  • 53