14
\$\begingroup\$

Sexy Primes are pairs of numbers \$(n, n+6)\$ such as \$n\$ and \$n+6\$ are both prime

You need to create a function which will take an integer, check for sexy primes from 0 to that integer, and return an array of arrays.

For example, listSexy(30) must return [[5,11], [7,13], [11,17], [13,19], [17,23], [23,29]] or some equivalent.

This is so the program with the shortest bytecount wins!

\$\endgroup\$
5
  • \$\begingroup\$ Can you explain what the desired output format looks like? \$\endgroup\$
    – Howard
    Jun 26, 2012 at 14:34
  • 13
    \$\begingroup\$ Bah, I hate it when the challenge changes after answers have been submitted. \$\endgroup\$
    – Griffin
    Jun 26, 2012 at 15:25
  • 3
    \$\begingroup\$ Is there a certain reason to check from 0? Why shouldn't I check from (5,11)? A short definition of sexy primes should be given right here, while linking to Wikipedia for further reading is welcome. \$\endgroup\$ Jun 27, 2012 at 14:45
  • 8
    \$\begingroup\$ In Portuguese Sexy Primes translates to the same as Sexy Cousins! \$\endgroup\$
    – sergiol
    Nov 6, 2017 at 23:35
  • \$\begingroup\$ @sergiol haha... \$\endgroup\$ Jul 28, 2020 at 10:50

33 Answers 33

1
2
1
\$\begingroup\$

CJam, 27 bytes

ri),{__6+_mp@mp&{pp}{;;}?}%

It prints pairs as two lines with one number each on then, largest to smallest. See the TIO for the example from the question.

Try it online

Explanation:

ri                              input an integer
  )                             increment
   ,{                    }%     iterate "m" from 0 to n-1
     __                         create two copies
       6+                       add 6
         _                      copy
          mp                    check if m+6 is prime
            @                   rotate so m is on top
             mp                 check if m is prime
               &                bitwise AND on two prime checks
                        ?       ternary if-else
                {pp}            if primes, print top two numbers
                    {;;}        otherwise, discard them
\$\endgroup\$
1
\$\begingroup\$

Excel, 131 bytes

=LET(x,SEQUENCE(A1),y,TRANSPOSE(x),z,x*(MMULT((MOD(x,y)=0)*1,x^0)=2),a,IFERROR(INDEX(z,x+{0,6}),),FILTER(a,MMULT(SIGN(a),{1;1})=2))

Link to Spreadsheet

Explanation

  • =LET( allows defined names within formulas.
  • x,SEQUENCE(A1), x = 1..A1 listed vertically.
  • y,TRANSPOSE(x), y = 1..A1 listed horizontally.
  • (MOD(x,y)=0)*1 creates a matrix this is 1 if x mod y = 0 and 0 otherwise.
  • MMULT(~,x^0) sums the elements of each row with matrix multiplication. x^0 is a vertical matrix of 1s.
  • z,x*(~=2), if the sum = 2 then x is prime and z=x else z=0.
  • ,a,IFERROR(INDEX(z,x+{0,6}),), creates an array a where the first column is z and the second is z offset by 6 rows.
  • FILTER(a,MMULT(SIGN(a),{1;1})=2) returns the rows of a that have no zeros.
\$\endgroup\$
0
\$\begingroup\$

C# ( 321 303 290 characters )

using System;namespace X{public class P{ static int l=100;static void Main(){F(0);}static bool I(int n){bool b=true;if(n==1){b=false;}for(int i=2;i<n;++i){if(n%i==0){b=false;break;}}return b;}static void F(int p){if((p+6)<=l){int m=p+6;if(I(m)&&I(p)){Console.WriteLine(m+","+p);}F(p+1);}}}}

Output:
11,5
13,7
17,11
19,13
23,17
29,23
37,31
43,37
47,41
53,47
59,53
67,61
73,67
79,73
89,83

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to CodeGolf! You can save some chars if you give the class and methods one-letter names (ex: class P instead of class Program). \$\endgroup\$ Jul 20, 2012 at 12:16
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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