Generate Sexy Primes

8

1

Sexy Primes are pairs of numbers (n, m) such as n and m both are prime, and m = n + 6.

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!

Rohit

Posted 2012-06-26T13:38:49.250

Reputation: 193

Can you explain what the desired output format looks like? – Howard – 2012-06-26T14:34:28.533

11Bah, I hate it when the challenge changes after answers have been submitted. – Griffin – 2012-06-26T15:25:53.907

2Is 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 – 2012-06-27T14:45:02.267

1In Portuguese Sexy Primes translates to the same as Sexy Cousins! – sergiol – 2017-11-06T23:35:23.090

Answers

11

MATLAB 32

i=1:n;i(isprime(i)&isprime(i+6))

n is your number

Griffin

Posted 2012-06-26T13:38:49.250

Reputation: 4 349

+1 That is the right tool for the job: isprime. Not that the op intended it. – Johannes Kuhn – 2013-11-19T07:34:49.450

5

J, 34 33 31 32 39 37 characters

s=.[:(,.-&6)[:I.1([:*/p:)"1 i.,.6-~i.

Lost a character keeping both primes below the limit...and another 7 declaring a function.

Usage:

   s 100
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

Edit

Seems like a lot of the new answers are not creating functions, taking input or limiting both numbers in the pair to be below n - if I ignore those restrictions too I can get down to 28 characters:

(,.6&+)I.*/"1[1 p:(i.,.6+i.)

Gareth

Posted 2012-06-26T13:38:49.250

Reputation: 11 678

5

Mathematica, 35

{#,#+6}&~Array~#~Cases~{__?PrimeQ}&

Mr.Wizard

Posted 2012-06-26T13:38:49.250

Reputation: 2 481

3

GolfScript, 32 characters

~),2>{:P{(.P\%}do(!},:L{6+L?)},p

Since the output format was not specified, the code above will print the lower prime of each pair. Thus a number x is included if x and x+6 are both prime and both are below n. Input is given as single number on STDIN.

> 150
[5 7 11 13 17 23 31 37 41 47 53 61 67 73 83 97 101 103 107 131]

Howard

Posted 2012-06-26T13:38:49.250

Reputation: 23 109

I think you should print both primes. It's a part of the task. – ugoren – 2012-06-27T08:45:08.790

1@ugoren The task was changed after I submitted my version. I'll try and add that to my version soon. – Howard – 2012-06-27T09:14:11.030

Yes, your answer matches the original task. – ugoren – 2012-06-27T09:25:52.747

2

Python (93 90 99 95)

Yay for quick and dirty isprime functions!

a=lambda x:all(x%i for i in range(2,x));b=2
while b<input():
 b+=1
 if a(b)&a(b+6):print b,b+6

beary605

Posted 2012-06-26T13:38:49.250

Reputation: 3 904

1 instead of True will save you 3 characters... – Wooble – 2012-06-26T17:54:54.413

Why do I keep missing that? Fixed. – beary605 – 2012-06-27T05:44:43.720

You don't get a limit parameter. Also, [] in all() isn't needed (at least in Python 2.7). – ugoren – 2012-06-27T08:43:36.357

Wow, really? Cool! I will implement the parameter. – beary605 – 2012-06-27T14:13:23.523

I don't know why you subtract 1 from x, but you can remove that for 2 characters. Then you can replace the while loop with a for loops from 2 to input()+1 for another 2 characters. – JPvdMerwe – 2012-07-20T22:33:33.030

2

K3 / Kona, 45

{a@&6=(-).'a:,/a,\:/:a:&{(x>1)&&/x!'2_!x}'!x}

.

{a@&6=(-).'a:,/a,\:/:a:&{(x>1)&&/x!'2_!x}'!x}100
(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)

And the same solution in the current incarnation of K which is identical to the K3 solution except for the fact that it does not have an inbuilt mod operator, which adds about 14 chars for 59

{a@&6=(-).'a:,/a,\:/:a:&{(x>1)&&/{x-y*x div y}[x;2_!x]}'!x}

tmartin

Posted 2012-06-26T13:38:49.250

Reputation: 3 917

1

C# (279 characters)

Basically, it's Saumil's solution with a couple of tweaks. I don't have enough any reputation for commenting though, so...

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

Mr Scapegrace

Posted 2012-06-26T13:38:49.250

Reputation: 221

1

J, 25 chars

(#~*/"1@p:~&1)(,+&6)"0 i.

i.n creates a range of [0,n)

(,+&6)"0 takes each integer n in the list and makes a pair n, n+6

(#~ condition) is basically a filter, and the condition in this case, */"1@p:~&1, just checks if a pair is composed solely of primes.

rationalis

Posted 2012-06-26T13:38:49.250

Reputation: 456

1

Octave 39

Modified my MATLAB answer to comply with the new (annoying) rules. n is your value.

p=@isprime;i=1:n;[j=i(p(i)&p(i+6));j+6]

Can be tested here

Griffin

Posted 2012-06-26T13:38:49.250

Reputation: 4 349

1

C, 102 99 95 chars

Returning an array in C is something you try to avoid. So the function s gets the limit n and a pointer to an array of integers, and fills it with the data. Each pair of sexy primes is placed in two positions in the array. So o[0]=5, o[1]=11, o[2]=7, o[3]=13. The function assumes the array is large enough.

x=7,l;
p(){
    return++l>=x/2||x*(x-6)%l&&p();
}
s(int n,int*o){
    for(;l=++x<=n;)p()?*o++=x-6,*o++=x:0;
}

ugoren

Posted 2012-06-26T13:38:49.250

Reputation: 16 527

1

R, 83 characters

f=function(n){library(gmp);p=isprime;for(i in 1:n)if(p(i)&p(i+6)){print(c(i,i+6))}}

Usage:

f(150)

Paolo

Posted 2012-06-26T13:38:49.250

Reputation: 696

1

Python, 137 132 126 122 116

I realise this is a bit of a fail, but it's my first answer, so why not.

f=lambda x:not[y for y in range(2,x)if x%y==0]
i=30
print [(x,y)for x in range(i)for y in range(i)if f(x)&f(y)&x-6==y]

Using list comprehensions, as well as the fact that [] = False

f(x) actually returns all the factors of x, and you can then work out prime-ness from that.

ACarter

Posted 2012-06-26T13:38:49.250

Reputation: 131

You can turn f(x) into f=lambda x:not[y for y in range(2,x)if x%y==0] to save a few characters. You can also reduce the ifs at the end of your list comprehension with f(x)&f(y)&(x-6==y). – beary605 – 2012-07-20T23:20:52.363

@beary605 I don't really know a lot about lamdas (well, I know nothing), so I'll look in to that, but yes, anda not ifs. – ACarter – 2012-07-27T21:32:03.253

A lambda is an anonymous function which returns a value. a=lambda x,y,z:(value here) is the same as def a(x,y,z):return (value here). – beary605 – 2012-07-28T03:21:45.660

Awesome, I seem to get the hang. Updated. Thanks! – ACarter – 2012-07-28T19:01:41.220

1

Ruby, 99 88 86 84 82 78

f=->x{(9..x).map{|n|[n-6,n]if[n,n-6].all?{|t|(2...t).all?{|m|t%m>0}}}.compact}

Sample output:

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

defhlt

Posted 2012-06-26T13:38:49.250

Reputation: 1 717

1

Ruby 75 74

The new version uses Artem Ice's prime test method:

z=->x{(9..x).map{|i|[i-6,i]}.select{|a|a.all?{|r|(2...r).all?{|m|r%m>0}}}}

Online test: http://ideone.com/yaOdn

Cristian Lupascu

Posted 2012-06-26T13:38:49.250

Reputation: 8 369

1

JavaScript (1 tweet = 140 Characters)

Here it is:

function t(n,i){for(i=2;i<n;i++)if(!(n%i))return!1;return!0}function s(n,p){for(p=[],i=2;i<n-6;i++)if(t(i)&&t(i+6))p.push([i,i+6]);return p}

Try s(30).

Inkbug

Posted 2012-06-26T13:38:49.250

Reputation: 468

0

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.

Titus

Posted 2012-06-26T13:38:49.250

Reputation: 13 814

0

Jelly, 13 bytes (non-competing)

‘Ḷµż+6$ÆPẠ$Ðf

Try it online!

Enhanced explanation:

‘Ḷµż+6$ÆPẠ$Ðf Main link. Arguments: z.
‘Ḷ            Range: [0..z].
  µ           Start a new monadic chain.
    +6        Add 6 to each element of x. (implicit x=⁸).
      $       Last two links (+6) as a monad.
   ż          Interleave x and y.
       ÆP     Do a primality check every element of every element of z.
         Ạ    Do an "all" check on every element of z.
          $   Last two links as a monad.
           Ðf Keep the elements of z that return a truthy value given this monad.

Erik the Outgolfer

Posted 2012-06-26T13:38:49.250

Reputation: 38 134

0

R 85 81 characters

f=function(n){m=2:n;a=m[rowSums(!outer(m,m,`%%`))<2];cbind(b<-a[(a+6)%in%a],b+6)}

Example run:

f(50)
      [,1] [,2]
 [1,]    5   11
 [2,]    7   13
 [3,]   11   17
 [4,]   13   19
 [5,]   17   23
 [6,]   23   29
 [7,]   31   37
 [8,]   37   43
 [9,]   41   47

plannapus

Posted 2012-06-26T13:38:49.250

Reputation: 8 610

0

Perl: 73 char

sub p{(1x$_[0])!~/^(11+?)\1+$/}map{$x=$_+6;p($_)&&p($x)&&say"$_,$x"}2..<>

usage:

echo 30 | perl -E 'sub p{(1x$_[0])!~/^(11+?)\1+$/}map{$x=$_+6;p($_)&&p($x)&&say"$_,$x"}2..<>'

output:

5,11
7,13
11,17
13,19
17,23
23,29

Toto

Posted 2012-06-26T13:38:49.250

Reputation: 909

0

Mathematica - 69 48 chars

Assuming m has been assigned a value

p=PrimeQ;Cases[Range@m,n_/;p@n&&p[n+6]:>{n,n+6}]

DavidC

Posted 2012-06-26T13:38:49.250

Reputation: 24 524

0

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

Saumil

Posted 2012-06-26T13:38:49.250

Reputation: 11

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). – Cristian Lupascu – 2012-07-20T12:16:16.800

0

C# 295

using System;using System.Linq;namespace K{class C{public static void Main(string[]a){Func<int,bool>p=i=>Enumerable.Range(2,i-3).All(x=>i%x>0);Console.WriteLine("["+String.Join(",",Enumerable.Range(0,int.Parse(a[0])).Where(i=>i>9&&p(i)&&p(i-6)).Select(i=>"["+(i-6)+","+i+"]").ToArray())+"]");}}}

Online test: http://ideone.com/4PwTW (in this test I've replaced int.Parse(a[0]) with the actual int value, as I cannot supply command line arguments to programs running on ideone.com)

Cristian Lupascu

Posted 2012-06-26T13:38:49.250

Reputation: 8 369

0

Scala (82)

def p(n:Int)=9 to n map(x=>List(x-6,x))filter(_.forall(l=>2 to l-1 forall(l%_>0)))

Sample output: Vector(List(5, 11), List(7, 13), List(11, 17), List(13, 19), List(17, 23), List(23, 29), List(31, 37), List(37, 43), List(41, 47), List(47, 53), List(53, 59), List(61, 67), List(67, 73), List(73, 79), List(83, 89))

defhlt

Posted 2012-06-26T13:38:49.250

Reputation: 1 717

0

Factor 140

This language is fun and interesting. My first script.

:: i ( n -- ? )
n 1 - 2 [a,b] [ n swap mod 0 > ] all? ;
:: s ( n -- r r )
11 n [a,b] [ i ] filter [ 6 - ] map [ i ] filter dup [ 6 + ] map ;

Usage:

( scratchpad ) 100 f

--- Data stack:
V{ 5 7 11 13 17 23 31 37 41 47 53 61 67 73 83 }
V{ 11 13 17 19 23 29 37 43 47 53 59 67 73 79 89 }

defhlt

Posted 2012-06-26T13:38:49.250

Reputation: 1 717

0

PARI/GP (62 characters)

f(a)=w=[];forprime(x=0,a,isprime(x+6)&w=concat(w,[[x,x+6]]));w

Example:

 (00:01) gp > f(a)=w=[];forprime(x=0,a,isprime(x+6)&w=concat(w,[[x,x+6]]));w
 (00:01) gp > f(30)
 %1 = [[5, 11], [7, 13], [11, 17], [13, 19], [17, 23], [23, 29]]

Yury

Posted 2012-06-26T13:38:49.250

Reputation: 131

0

Haskell (65 chars)

p n=[(x,x+6)|x<-[3..n-6],all(\k->all((>0).mod k)[2..k-1])[x,x+6]]

The output:

Prelude> p 100
[(5,11),(7,13),(11,17),(13,19),(17,23),(23,29),(31,37),(37,43),(41,47),(47,53),(
53,59),(61,67),(67,73),(73,79),(83,89)]

About the MATLAB answer here:

(I spent all my rep on a bounty so can't comment just yet). Google says: " the Matlab's isprime function ... is based on the probabilistic Miller-Rabin". So it seems that the MATLAB entry should be disqualified.

Will Ness

Posted 2012-06-26T13:38:49.250

Reputation: 352

-3

Obj-C 64 chars

if([self isPrime:i]&&[self isPrime:i+6])NSLog(@"%d %d\n",i,i+6);

isPrime implemented separately

akshay1188

Posted 2012-06-26T13:38:49.250

Reputation: 101

6If you're declaring a function isPrime which is not part of the language or standard library, you need to include the character count for that function as part of your score. – Gareth – 2012-06-26T15:19:22.550