Why is my prime check function returning true on multiples of 5?

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
}


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation