forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwinPrime.js
More file actions
Latest commit
29 lines (24 loc) · 544 Bytes
/
Copy pathTwinPrime.js
File metadata and controls
29 lines (24 loc) · 544 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
*/
functiontwinPrime(n){
constprime=PrimeCheck(n)
if(!prime){
return-1
}
if(!PrimeCheck(n+2)){
return-1
}
returnn+2
}
export{twinPrime}