We define twin primes as two natural numbers p,p+2 which both are prime.

Example: 5 and 7 are twin primes.

Let's define the twin number of some set of numbers as the number of twin prime numbers in that set

Example: {6,7,11,13,18,29,31} has a twin number 4, since there are four twin primes; 11,13 and 29,31.


Input: A number n

Output: The twin number of the set of all natural numbers below n


This is , so lowest bytes wins.

  • 8
    \$\begingroup\$ Do you have some test cases? \$\endgroup\$
    – Suever
    Commented Jun 14, 2016 at 0:42
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ Natural number must also be whole numbers, and I assume he only wants us to deal with whole number primes \$\endgroup\$
    – bren
    Commented Jun 14, 2016 at 1:06
  • 5
    \$\begingroup\$ I'm not sure I understand the definition. The example set seems to have 4 twin primes. Are we supposed to count the number of pairs? If so, does 3, 5, 7 count as two pairs? What if one of the primes is in the set, but the other one isn't? \$\endgroup\$
    – Dennis
    Commented Jun 14, 2016 at 1:12
  • 1
    \$\begingroup\$ OK, I'm reopening, but a few test cases would still be helpful. \$\endgroup\$
    – Dennis
    Commented Jun 14, 2016 at 1:25
  • 3
    \$\begingroup\$ I think this is a duplicate of Find the Nth pair of twin primes. This one has you count them and that one has you generate them counting up, but the core process of enumerating and checking twin primes is the same. \$\endgroup\$
    – xnor
    Commented Jun 14, 2016 at 2:42

4 Answers 4


MATL, 7 bytes


This solution exploits the fact that the prime gap (the difference between successsive prime numbers) is always >= 2 (with the exception being the gap between 2 and 3). Because of this fact, all twin primes will be successive prime numbers. In this solution we just compute the differences between all primes less than the input n and count how many of these differences are equal to 2.

Try it Online


    % Implicitly grab input, N
q   % Subtract one from the input (N-1)
Zq  % Get an array of all primes <= (N-1)
d   % Compute successive differences (prime gaps)
2=  % Create a boolean array indicating which prime gaps are equal to 2
s   % Count the TRUE values in this array to determine the twin number
    % Implicitly display the result

If you instead want the not the number of pairs but the number of primes that are part of a pair, the following would work


Try it Online


Jolf, 10 bytes


Try it here!


    ~B     all numbers satisfying
       ©{  the prime condition ("the prime directive"?)
      x    below x
  Zd       (the differences between indices)
Zl       2 count the number of 2's

Ruby, 43 + 8 (-rprime flag) = 51 bytes


Perl 6,  54  53 bytes

{2*(3..$_).rotor(3=>-1).grep: {all .[0,2]».is-prime}}
{2*(3,5...$_).rotor(2=>-1).grep: {all $_».is-prime}}


  (3,5...$_)        # odd numbers
  .rotor( 2 => -1 ) # grab 2 then backup one, repeat
  .grep:            # find all that match the following
      # create a Junction object of the following

        # call .is-prime on both elements of the
        # list given to this inner block

Not the answer you're looking for? Browse other questions tagged or ask your own question.