Skip to main content
4 of 5
added 519 characters in body
Dennis
  • 210.6k
  • 41
  • 370
  • 825

Python 3, 53 bytes

lambda n:sum((n+n%6-3)*n%k<1for k in range(2,4*n))==2

Try it online!

Background

All integers take one of the following forms, with integer k: 6k - 3, 6k - 2, 6k - 1, 6k, 6k + 1, 6k + 2.

Since 6k - 2, 6k, and 6k + 2 are all even, and since 6k - 3 is divisible by 3, all primes except 2 and 3 must be of the form 6k - 1 or 6k + 1. Since the difference of a twin prime pair is 2, with the exception of (3, 5), all twin prime pairs are of the form (6k - 1, 6k + 1).

Let n be of the form 6k ± 1.

  • If n = 6k -1, then n + n%6 - 3 = 6k - 1 + (6k - 1)%6 - 3 = 6k - 1 + 5 - 3 = 6k + 1.

  • If n = 6k + 1, then n + n%6 - 3 = 6k + 1 + (6k + 1)%6 - 3 = 6k + 1 + 1 - 3 = 6k - 1.

Thus, if n is part of a twin prime pair and n ≠ 3, it's twin will be n + n%6 - 3.

How it works (WIP)

Python doesn't have a built-in primality test. While there are short-ish ways to test a single number for primality, doing so for two number would be lengthy. We're going to work with divisors instead.

sum((n+n%6-3)*n%k<1for k in range(2,4*n))

counts how many integers k in the interval [2, 4n) divide (n + n%6 - 3)n evenly, i.e., it counts the number of divisors of (n + n%6 - 3)n in the interval [2, 4n). We claim that this count is 2 if and only if n is part of a twin prime pair.

  • If n = 1, the integer (n + n%6 - 3)n = 1 + 1 - 3 = -1 has no divisors in [2, 4).

  • If n = 2, the integer (n + n%6 - 3)n = 2(2 + 2 - 3) = 2 has one divisor (itself) in [2, 8).

  • If n = 3 (a twin prime), the integer (n + n%6 - 3)n = 3(3 + 3 - 3) = 9 has two divisors (3 and 9) in [2, 12).

  • If n = 4, the integer (n + n%6 - 3)n = 4(4 + 4 - 3) = 20 has four divisors (2, 4, 5, 10) in [2, 16).

  • If n = 6, the integer (n + n%6 - 3)n = 6(6 + 0 - 3) = 18 has five divisors (2, 3, 6, 9, 18) in [2, 24).

  • If n > 3 is a twin prime, as seen before, m := n + n%6 - 3 is its twin. In this case, mn has exactly four divisors: 1, m, n, mn.

    Since n > 3, we have m > 4, so 4n < mn and exactly two divisors (m and n) fall into the interval [2, 4n).

Dennis
  • 210.6k
  • 41
  • 370
  • 825