Twin Prime

import { PrimeCheck } from './PrimeCheck'

/**
 * @function twinPrime
 * Gets the 'twin prime' of a prime number.
 *
 * @param {Integer} n The number to find the twin prime of.
 * @returns {Integer} Either the twin, or -1 if n or n + 2 is not prime.
 *
 * @see https://en.wikipedia.org/wiki/Twin_prime
 *
 * @example twinPrime(5) = 7
 * @example twinPrime(4) = -1
*/
function twinPrime (n) {
  const prime = PrimeCheck(n)

  if (!prime) {
    return -1
  }

  if (!PrimeCheck(n + 2)) {
    return -1
  }

  return n + 2
}

export { twinPrime }
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.