Guest User

Untitled

a guest
Dec 17th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. /* A twin prime is a prime number that differs from another prime number by two. Write a function called isTwinPrime which takes an integer and returns true if it is a twin prime, or false if it is not.
  2. Example:
  3.  
  4. 5 is a prime, and 5 + 2 = 7, which is also a prime, so returns true.
  5. 9 is not a prime, and so does not need checking, so it returns false.
  6. 7 is a prime, but 7 + 2 = 9, which is not a prime. However, 7 - 2 = 5, which is a prime, so it returns true.
  7. 23 is a prime, but 23 + 2 is 25, which is not a prime. 23 - 2 is 21, which isn't a prime either, so 23 is not a twin prime.*/
  8.  
  9. function isTwinPrime(n) {
  10. function isPrime(num) {
  11. for (let i = 2; i <= Math.sqrt(num); i++) {
  12. if (num % i === 0) return false;
  13. }
  14. return num >= 2;
  15. }
  16. return (isPrime(n) && (isPrime(n - 2) || isPrime(n + 2)));
  17. }
Add Comment
Please, Sign In to add comment