Sexy primes: Difference between revisions

→‎{{header|REXX}}: added the REXX computer programming language.
(→‎{{header|zkl}}: added code)
(→‎{{header|REXX}}: added the REXX computer programming language.)
Line 267:
Number of unsexy primes less than 1,000,000: 48,626
Last 10 unsexy primes less than 1,000,000: (999809 999853 999863 999883 999907 999917 999931 999961 999979 999983)</pre>
 
=={{header|REXX}}==
<lang rexx>/*REXX program finds/displays various kinds of sexy and unsexy primes up to N inclusive.*/
parse arg N endU end2 end3 end4 end5 . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 1000000 /*Not specified? Then use the default.*/
if endU=='' | endU=="," then endU= 10 /* " " " " " " */
if end2=='' | end2=="," then end2= 5 /* " " " " " " */
if end3=='' | end3=="," then end3= 5 /* " " " " " " */
if end4=='' | end4=="," then end4= 5 /* " " " " " " */
if end5=='' | end5=="," then end4= 5 /* " " " " " " */
@.=.; s.=0; #=0; !.=0 /*count of primes (so far); set prime#.*/
if N>1 then do; #=1; @.1=2; !.2=1; end /*a count of the primes found (so far).*/
 
do j=3 by 2 to N+6 /*start in the cellar and work up. */
if j<19 then if wordpos(j, '3 5 7 11 13 17')==0 then iterate
else do; #= #+1; @.#= j; !.j= 1
b= j-6; if !.b then s.b= 1
iterate
end
if j// 3 ==0 then iterate /* ··· and eliminate the triples. */
parse var j '' -1 _ /* obtain the rightmost digit.*/
if _ ==5 then iterate /* ··· and eliminate the nickels. */
if j// 7 ==0 then iterate /* ··· and eliminate the luckies. */
do k=11 by 6 while k*k<=j /*this skips odd multiples of three. */
if j//k ==0 then iterate j /*perform a divide (modulus), */
if j//(k+2)==0 then iterate j /* ··· and the next also. ___ */
end /*k*/ /*divide up through the √ J */
if j<=N then do; #= #+1; @.#= j; !.j= 1 /*bump prime counter; assign prime to @*/
end /* [+] also, assign prime# to unity. */
!.j= 1 /*assign Jth number as being prime. */
b= j - 6 /*B: is the lower part of a sexy prime*/
if !.b then do; s.b=1; if j<=N then s.j=1 /*assign (maybe both) sexy primes. */
end /* [↑] is this part of a sexy prime ? */
end /*j*/
u= 2 /*the first unsafe sexy prime. */
Nc= commas(N)
say "There are " commas(#) " primes up to " Nc ' (inclusive).'
s=
do k=2 for #-1; p= @.k
if s.p then s= s p
else u= u p
end /*k*/
say
uu= words(u)
s1= words(s)
s2= /* [↓] build sexy prime pairs. */
do k=2 for #-1; p= @.k; if \s.p then iterate
b= p - 6; if \s.b then iterate
s2= s2 b'~'p
end /*k*/
s3= /* [↓] build sexy prime triplets. */
do k=2 for #-1; p= @.k; if \s.p then iterate
b= p - 6; if \s.b then iterate
t= p - 12; if \s.t then iterate
s3= s3 t'~' || b"~"p
end /*k*/
s4= /* [↓] build sexy prime quadruplets. */
do k=2 for #-1; p= @.k; if \s.p then iterate
b= p - 6; if \s.b then iterate
t= p - 12; if \s.t then iterate
q= p - 18; if \s.q then iterate
s4= s4 q'~'t"~" || b'~'p
end /*k*/
s5= /* [↓] build sexy prime quintuplets. */
do k=2 for #-1; p= @.k; if \s.p then iterate
b= p - 6; if \s.b then iterate
t= p - 12; if \s.t then iterate
q= p - 18; if \s.q then iterate
v= p - 24; if \s.v then iterate
s5= s5 v'~'q"~"t'~' || b"~"p
end /*k*/
@sexy= ' sexy prime' /*handy literal for SAYs.*/
w2= words( translate(s2,, '~') ); y2= words(s2) /*count # primes in the sexy pairs. */
w3= words( translate(s3,, '~') ); y3= words(s3) /* " " " " " " triplets. */
w4= words( translate(s4,, '~') ); y4= words(s4) /* " " " " " " quads. */
w5= words( translate(s5,, '~') ); y5= words(s5) /* " " " " " " quints. */
say 'There are ' commas(w2%2) @sexy "pairs up to " Nc ' (inclusive).'
say 'The last ' commas(end2) @sexy "pairs are:"; say subword(s2, max(1,y2-end2+1))
say
say 'There are ' commas(w3%3) @sexy "triplets up to " Nc ' (inclusive).'
say 'The last ' commas(end3) @sexy "triplets are:"; say subword(s3, max(1,y3-end3+1))
say
say 'There are ' commas(w4%4) @sexy "quadruplets up to " Nc ' (inclusive).'
say 'The last ' commas(end4) @sexy "quadruplets are:"; say subword(s4, max(1,y4-end4+1))
say
say 'There are ' commas(w5%5) @sexy "quintuplets up to " Nc ' (inclusive).'
say 'The last ' commas(end4) @sexy "quintuplets are:"; say subword(s5, max(1,y5-end4+1))
say
say 'There are ' commas(s1) " sexy primes up to " Nc ' (inclusive).'
say 'There are ' commas(uu) " unsexy primes up to " Nc ' (inclusive).'
say 'The last ' commas(endU) " unsexy primes are: " subword(u, max(1,uu-endU+1))
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: procedure; parse arg _; n= _'.9'; #= 123456789; b= verify(n, #, "M")
e= verify(n, #'0', , verify(n, #"0.", 'M') ) - 4
do j=e to b by -3; _= insert(',', _, j); end /*j*/; return _</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
There are 78,498 primes up to 1,000,000 (inclusive).
 
There are 16,386 sexy prime pairs up to 1,000,000 (inclusive).
The last 5 sexy prime pairs are:
999371~999377 999431~999437 999721~999727 999763~999769 999953~999959
 
There are 2,900 sexy prime triplets up to 1,000,000 (inclusive).
The last 5 sexy prime triplets are:
997427~997433~997439 997541~997547~997553 998071~998077~998083 998617~998623~998629 998737~998743~998749
 
There are 325 sexy prime quadruplets up to 1,000,000 (inclusive).
The last 5 sexy prime quadruplets are:
977351~977357~977363~977369 983771~983777~983783~983789 986131~986137~986143~986149 990371~990377~990383~990389 997091~997097~997103~997109
 
There are 1 sexy prime quintuplets up to 1,000,000 (inclusive).
The last 5 sexy prime quintuplets are:
5~11~17~23~29
 
There are 29,872 sexy primes up to 1,000,000 (inclusive).
There are 48,626 unsexy primes up to 1,000,000 (inclusive).
The last 10 unsexy primes are: 999809 999853 999863 999883 999907 999917 999931 999961 999979 999983
</pre>
 
=={{header|zkl}}==