# Generate Sexy Primes

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!

• Can you explain what the desired output format looks like? – Howard Jun 26 '12 at 14:34
• Bah, I hate it when the challenge changes after answers have been submitted. – Griffin Jun 26 '12 at 15:25
• 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. – user unknown Jun 27 '12 at 14:45
• In Portuguese Sexy Primes translates to the same as Sexy Cousins! – sergiol Nov 6 '17 at 23:35
• @sergiol haha... – Victor VosMottor Jul 28 '20 at 10:50

# PHP, 106 bytes

function p($n){for($i=$n;--$i&&$n%$i;);return$i-1;}for(;++$i<$argv[1]-5;)p($i)|p($k=$i+6)?:print"$i,$k\n";


program prints pairs as n,n+6 delimited by linebreaks. Run with -r.

I modified my is_prime function (& saved a byte) so that it returns 0 for primes to golf on the Elvis.

# 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
_                      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


# JavaScript (V8), 104 bytes

(x,p=[])=>[...Array(x-6)].map((_,i)=>(f=(n,t=2)=>t<n?n%t?f(n,t+1):0:n>1)(i)&&f(i+6)&&p.push([i,i+6]))&&p


Try it online!

# 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))


• =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.