| Bytes | Lang | Time | Link |
|---|---|---|---|
| 046 | TIBASIC TI83 Plus | 250411T142357Z | madeforl |
| 084 | AWK | 250409T180642Z | xrs |
| 011 | Japt | 221122T211846Z | Shaggy |
| 101 | POSIX Shell Command Language + Utilities + seq + 4.1BSD factor | 221122T203419Z | набиячлэ |
| 061 | Ruby | 221122T200222Z | Jordan |
| 075 | JavaScript V8 | 210507T220547Z | rydwolf |
| 131 | Excel | 210521T151933Z | Axuary |
| 009 | Jelly | 210521T133254Z | caird co |
| 1210 | Brachylog | 200708T145610Z | xash |
| 027 | CJam | 200712T183906Z | Ethan Ch |
| 118 | Python 2 | 120707T184135Z | ACarter |
| 010 | 05AB1E | 200705T072756Z | user9206 |
| 007 | 05AB1E | 200707T132730Z | Kevin Cr |
| 8678 | x86Assembly 78 Bytes | 200705T102553Z | fcdt |
| 113 | JavaScript 113 Characters | 120801T113146Z | Inkbug |
| 279 | C# | 161220T081245Z | Mr Scape |
| 106 | PHP | 161220T030003Z | Titus |
| 025 | J | 131119T054929Z | rational |
| 081 | R | 131115T074915Z | plannapu |
| 065 | Haskell | 120801T174022Z | Will Nes |
| 290 | C# | 120718T220814Z | Saumil |
| 035 | Mathematica | 120729T125506Z | Mr.Wizar |
| 062 | PARI/GP | 120724T050752Z | Yury |
| 140 | Factor | 120723T043142Z | defhlt |
| 082 | Scala | 120721T033546Z | defhlt |
| 078 | Ruby | 120716T204824Z | defhlt |
| 095 | Python | 120626T153348Z | beary605 |
| 048 | Mathematica | 120627T002609Z | DavidC |
| 074 | Ruby | 120720T115943Z | Cristian |
| 295 | C# | 120720T124658Z | Cristian |
| 045 | K3 / Kona | 120626T153408Z | tmartin |
| 083 | R | 120627T075942Z | Paolo |
| 095 | C | 120627T064247Z | ugoren |
| 037 | J | 120626T142149Z | Gareth |
| 039 | Octave | 120627T005250Z | Griffin |
| 073 | Perl | 120626T174715Z | Toto |
| 032 | GolfScript | 120626T143355Z | Howard |
| 032 | MATLAB | 120626T140716Z | Griffin |
TI-BASIC (TI-83 Plus), 46 bytes
Input A
For(C,4,A
If min(seq(fPart(C/K) and fPart((C+6)/K),K,2,C-1
Disp {C,C+6
End
AWK, 84 bytes
func f(x){for(j=1;x%++j;);return j~x}{for(i=4;i++<$1-6;)if(f(i)&&f(i+6))print i,i+6}
func f(x){ # prime function
for(j=1;x%++j;); # prime test
return j~x} # return 1 if prime
{for(i=4;i++<$1-6;) # loop till ceiling
if(f(i)&&f(i+6)) # both numbers prime?
print i,i+6} # print
POSIX Shell Command Language + Utilities + seq + 4.1BSD factor, 101 bytes
seq 2 $1|factor|sed '/: .* /d;s/:.*//'>q
while read a;do grep -x $((a+6)) q&&echo $a;done<q|paste - -
seq generates integers in [2, specified], factor turns them into this format we all know and love:
2: 2
3: 3
4: 2 2
5: 5
6: 2 3
sed deletes lines with a space after the first colon-space (i.e. non-primes), then trims off everything after the colon. For each prime a, we copy the prime equal to a+6 to the standard output stream, and if that succeeded, also write a, then we paste each pair of output lines together.
Any seq since its introduction in Version 8 AT&T UNIX works, and you can trivially replace it with a while at a cost of 42 bytes. Any factor since its introduction in Version 7 AT&T UNIX works (sans format quirks maybe, I'm not in the mood for reading PDP-11 assembly; if the format is space-less, then 4.1BSD probably has the right one).
Transcript of test cases:
$ cat d.sh; echo; wc -c d.sh
seq 2 $1|factor|sed '/: .* /d;s/:.*//'>q
while read a;do grep -x $((a+6)) q&&echo $a;done<q|paste - -
101 d.sh
$ ./d.sh 30
11 5
13 7
17 11
19 13
23 17
29 23
Herein an "array of arrays" is a set of lines containing tab-delimited fields.
JavaScript (V8), 104 75 bytes
-29 bytes thanks to @emanresuA
g=x=>--x>6&&g(x,(f=(n,t=2)=>t<n?n%t&&f(n,t+1):n>1)(x)*f(x-6)&&print(x-6,x))
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))
Explanation
=LET(allows defined names within formulas.x,SEQUENCE(A1),x =1..A1listed vertically.y,TRANSPOSE(x),y =1..A1listed horizontally.(MOD(x,y)=0)*1creates 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^0is a vertical matrix of 1s.z,x*(~=2),if the sum = 2 thenxis prime andz=xelsez=0.,a,IFERROR(INDEX(z,x+{0,6}),),creates an arrayawhere the first column iszand the second iszoffset by 6 rows.FILTER(a,MMULT(SIGN(a),{1;1})=2)returns the rows ofathat have no zeros.
Jelly, 9 bytes
Rż+¥6ẒẠ$Ƈ
How it works
Rż+¥6ẒẠ$Ƈ - Main link. Takes N on the left
R - Range; R = [1, 2, ..., N]
¥6 - Previous 2 links as a dyad f(R, 6):
+ - Add 6 to each element in R
ż - Zip together
$Ƈ - Keep those pairs for which the following is true:
Ạ - Are all elements...
Ẓ - ...prime?
Brachylog, 12 10 bytes
-2 thanks to @Fatalize
≥Iṗ-₆ṗ;I≜ᶠ
How it works
≥Iṗ-₆ṗ;I≜ᶠ
ᶠ find all …
≜ force number-finding of constraints that fulfill …
(strictly not necessary, but otherwise constraints will be returned)
≥ any number less than the input
I assign it to I
ṗ must be prime
-₆ṗ minus 6 must also be prime
;I append I again (so we have [I-6,I])
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.
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
Python 2, 137 132 126 122 118
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.
05AB1E, 10 bytes
Outputs in reversed order, because the K3 answer also seems to do that.
LεD6-‚}ʒpP
Explanation
L Length range
ε Map:
D Duplicate
6- Minus 6
‚ Pair
} Reverse the pair
ʒ Filter:
p Is_prime
P in both items
05AB1E, 7 bytes
ÅPãʒÆ7+
Try it online or verify a few more test cases.
Explanation:
ÅP # Push a list of primes smaller than or equal to the (implicit) input-integer
ã # Create all possible pairs by taking the cartesian product with itself
# (which will include inverted duplicates - i.e. both [5,11] and [11,5])
ʒ # Filter this list of pairs [a,b] by:
Æ # Reduce it by subtracting: a-b
7+ # Add 7
# (only 1 is truthy in 05AB1E, so this will keep pairs that satisfy a-b+7==1)
# (after which the resulting list of pairs is output implicitly)
x86-Assembly (78 Bytes)
This example uses Intel syntax. Besides, here the fastcall or register calling convention is used (the latter from 32-bit Delphi), so the parameter is in ecx. I also introduce a second parameter that points to a free space for the result and is saved in edx. A function for allocating memory space would be specific to the operating system and a transfer via the stack would be problematic, so I chose this option.
listSexy:
mov ebp, esp ; 89E5 Save initial esp
mov edi, edx ; 89D7 Save second parameter in edx
; Part One: Save all prime numbers from 3 to the limit on the stack
; (This takes 32 Bytes)
xor eax, eax ; 31C0 set eax = 1 (shorter than mov eax, 1)
inc eax ; 40
checkComplete:
add eax, 2 ; 83C002 Go to next possible number (first is 3)
cmp eax, ecx ; 39C8 Has the limit, i.e. the parameter, been skipped?
jae partTwo ; 7316
mov ebx, esp ; 89E3 Check it against all previous prime numbers
checkNext: ; which are stored between ebp and esp
cdq ; 99 Clear edx for the later division
push eax ; 50 Put the number on the stack
cmp ebx, ebp ; 39E5 Has the check already completed?
je partTwo ; 74F1 The comfirmed prime number remains on stack
mov esi, [ebx] ; 8B33 Load a previous prime
div esi ; F7F6
pop eax ; 58
test edx, edx ; 85D2 Is esi a divider?
je checkComplete ; 74E8
add ebx, 4 ; 83C304 Let ebx point to the next possible divisor
jmp checkNext ; EBEC
; Part Zwo: Check all prime numbers if they are sexy primes
; (This takes 39 Bytes)
partTwo:
mov ebx, ebp ; 89E5 Start with the first prime number, i.e. 3
NextPrime:
sub ebx, 4 ; 83EB04 Let ebx point to the next prime number
cmp ebx, esp ; 39E3 Has the last number been reached? It cant be sexy,
; since it has no successors here
jmp Finished ; 761E
mov edx, ebx ; 89DA Check all successors up to a distance of 6
mov eax, [ebx] ; 8B03 eax is the prime number whose successors are checked
add eax, 6 ; 83C006
GoFurther:
sub edx, 4 ; 83EA04
cmp eax, [edx] ; 8B02
jb NextPrime ; 72EB If the successor is too large, x can no longer be sexy
ja GoFurther ; 77F7 If the successor is too small, take the next one
mov eax, [ebx] ; 8B03 Load eax again (shorter than sub eax, 6)
mov ecx, [edx] ; 8B0A Load the successor which makes eax a sexy prime
mov [edi], eax ; 8907 Store both in their intended place
mov [edi+4], ecx ; 894F04
add edi, 8 ; 83C708
jmp NextPrime ; EBDB
Finished:
mov esp, ebp ; 89EC Restore stack
ret ; C3
The opcodes (and thus the size of the instruction) are given in the comment. The function can be called from C with, for example
int n = 100;
int resultList[50] = { };
listSexy(n, resultList);
The result list should be initialized with zero, since no final null byte sequence or similar is written into.
Online working example: http://tpcg.io/mEPb1Wy9
JavaScript (140 113 Characters)
t=(i,n)=>{for(n=i;n%--i;);return 1==i};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).
Updated to use the shorter primality test from here, and to use the new arrow notation.
Old version (140 characters):
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}
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
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.
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.
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
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.
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
Mathematica, 35
{#,#+6}&~Array~#~Cases~{__?PrimeQ}&
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]]
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 }
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))
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]]
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
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}]
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
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)
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}
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)
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;
}
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.)
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
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
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]
MATLAB 32
i=1:n;i(isprime(i)&isprime(i+6))
n is your number