Sexy primes: Difference between revisions

Added J implementation
No edit summary
(Added J implementation)
Line 949:
display :: Show a => [a] -> String
display = show . reverse</lang>
 
=={{header|J}}==
<lang j>SEX =: 6 NB. Lol, latin
ORGY_SIZE =: 5 NB. Max group size
 
NB. Primes Not Greater Than (the input)
NB. The 1 _1 p: ... logic here allows the input value to
NB. be included in the list in the case it itself is prime
pngt =: p:@:i.@:([: +/ 1 _1 p:"0 ])
 
NB. Add 6 and see which sums appear in input list
sexy =: #~ SEX&+ e. ]
 
NB. Iterate "sexy" logic up to orgy size
orgy =: sexy&.>^:(<ORGY_SIZE)@:<
 
sp =: dyad define
's os' =. x NB. prime distance, group size
p =. pngt y
o =. orgy p
g =. o +/&.> <\ +/\ _1 |.!.0 os # s NB. Groups
 
's g' =. split g NB. Split singles from groups
l =. (({.~ -) 5 <. #)&.> g NB. Last (max) 5 groups
 
( (#&.> g) ,. l ) ,~ (# ; _10&{.) }: p -. s NB. Unsexy numbers; }: is to exclude 1000033 per spec
)</lang>
 
{{out}}
<lang j>
r =: 1000035 sp~ SEX,ORGY_SIZE
(;:'Group Count Examples') , (;:'Unsexy Pair Triplets Quadruplets Quintuplets') ,. r
+-----------+-----+----------------------------------------------------------------------+
|Group |Count|Examples |
+-----------+-----+----------------------------------------------------------------------+
|Unsexy |78499|999883 999907 999917 999931 999953 999959 999961 999979 999983 1000003|
+-----------+-----+----------------------------------------------------------------------+
|Pair |16386|999371 999377 |
| | |999431 999437 |
| | |999721 999727 |
| | |999763 999769 |
| | |999953 999959 |
+-----------+-----+----------------------------------------------------------------------+
|Triplets |2900 |997427 997433 997439 |
| | |997541 997547 997553 |
| | |998071 998077 998083 |
| | |998617 998623 998629 |
| | |998737 998743 998749 |
+-----------+-----+----------------------------------------------------------------------+
|Quadruplets|325 |977351 977357 977363 977369 |
| | |983771 983777 983783 983789 |
| | |986131 986137 986143 986149 |
| | |990371 990377 990383 990389 |
| | |997091 997097 997103 997109 |
+-----------+-----+----------------------------------------------------------------------+
|Quintuplets|1 |5 11 17 23 29 |
+-----------+-----+----------------------------------------------------------------------+</lang>
 
 
=={{header|Java}}==
Anonymous user