Sexy primes: Difference between revisions

55,741 bytes added ,  22 days ago
Added BASIC256 and Yabasic. Moved FreeBASIC to section BASIC
(Added BASIC256 and Yabasic. Moved FreeBASIC to section BASIC)
 
(41 intermediate revisions by 18 users not shown)
Line 28:
::*Note that 1000033 '''SHOULD NOT''' be counted in the pair count. It is sexy, but not in a pair within the limit. However, it also '''SHOULD NOT''' be listed in the unsexy primes since it is sexy.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V LIMIT = 1'000'000
F get_primes(limit)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
I is_prime[n]
L(i) (n * n .< limit + 1).step(n)
is_prime[i] = 0B
R enumerate(is_prime).filter((i, prime) -> prime).map((i, prime) -> i)
 
V primes = get_primes(LIMIT)
V primeset = Set(primes)
 
V s = [[[Int]]()] * 4
[Int] unsexy
L(p) primes
I p + 6 C primeset
s[0].append([p, p + 6])
E
I p - 6 !C primeset
unsexy.append(p)
L.continue
I p + 12 C primeset
s[1].append([p, p + 6, p + 12])
E
L.continue
I p + 18 C primeset
s[2].append([p, p + 6, p + 12, p + 18])
E
L.continue
I p + 24 C primeset
s[3].append([p, p + 6, p + 12, p + 18, p + 24])
 
print(‘"SEXY" PRIME GROUPINGS:’)
L(sexy, name) zip(s, ‘pairs triplets quadruplets quintuplets’.split(‘ ’))
print(‘ #. #. ending with ...’.format(sexy.len, name))
L(sx) sexy[(len)-5..]
print(‘ ’sx)
 
print("\nThere are #. unsexy primes ending with ...".format(unsexy.len))
L(usx) unsexy[(len)-10..]
print(‘ ’usx)</syntaxhighlight>
 
{{out}}
<pre>
"SEXY" PRIME GROUPINGS:
16386 pairs ending with ...
[999371, 999377]
[999431, 999437]
[999721, 999727]
[999763, 999769]
[999953, 999959]
2900 triplets ending with ...
[997427, 997433, 997439]
[997541, 997547, 997553]
[998071, 998077, 998083]
[998617, 998623, 998629]
[998737, 998743, 998749]
325 quadruplets ending with ...
[977351, 977357, 977363, 977369]
[983771, 983777, 983783, 983789]
[986131, 986137, 986143, 986149]
[990371, 990377, 990383, 990389]
[997091, 997097, 997103, 997109]
1 quintuplets ending with ...
[5, 11, 17, 23, 29]
 
There are 48626 unsexy primes ending with ...
999809
999853
999863
999883
999907
999917
999931
999961
999979
999983
</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin
% find some sexy primes - primes that differ from another prime by 6 %
% implements the sieve of Eratosthenes %
procedure sieve( logical array s ( * ); integer value n ) ;
begin
% start with everything flagged as prime %
for i := 1 until n do s( i ) := true;
% sieve out the non-primes %
s( 1 ) := false;
for i := 2 until truncate( sqrt( n ) ) do begin
if s( i ) then for p := i * i step i until n do s( p ) := false
end for_i ;
end sieve ;
% adds a prime to list of sexy/unsexy primes %
procedure addPrime ( integer value p
; integer array list ( * )
; integer value len
) ;
begin
% increment count, shuffle down the primes and add the new one %
list( 0 ) := list( 0 ) + 1;
for i := 1 until len - 1 do list( i ) := list( i + 1 );
list( len ) := p
end addPrime ;
% counts the number of pairs of sexy primes, triplets, quadruplest and %
% quintuplets up to n %
% the counts of each kind are returned in the 0 element of the arrays %
% the last 5 ( or less if there are less than 5 ) of each type of sexy %
% prime is returned in the array elements 1 to 5 %
procedure countSexyPrimes ( logical array s ( * )
; integer value n
; integer array pairs, triplets, quadruplets, quintuplets ( * )
) ;
begin
integer pos2, pos3, pos4, pos5;
for i := 0 until 5 do pairs( i ) := triplets( i ) := quadruplets( i ) := quintuplets( i ) := 0;
% look for pairs etc. up to n %
% 2 cannot be a sexy prime as it is the only even prime, thus: %
% pairs can start at 7, triplets at 13, quadruplets at 19 and %
% quintuplets at 25 %
for p := 7 step 2 until 11 do begin
if s( p ) and s( p - 6 ) then addPrime( p, pairs, 5 )
end for_p ;
for p := 13 step 2 until 17 do begin
if s( p ) and s( p - 6 ) then addPrime( p, pairs, 5 );
if s( p ) and s( p - 6 ) and s( p - 12 ) then addPrime( p, triplets, 5 )
end for_p ;
for p := 19 step 2 until 23 do begin
if s( p ) and s( p - 6 ) then addPrime( p, pairs, 5 );
if s( p ) and s( p - 6 ) and s( p - 12 ) then addPrime( p, triplets, 5 );
if s( p ) and s( p - 6 ) and s( p - 12 ) and s( p - 18 )
then addPrime( p, quadruplets, 5 )
end for_p ;
pos5 := 1;
pos4 := pos5 + 6;
pos3 := pos4 + 6;
pos2 := pos3 + 6;
for p := pos2 + 6 step 2 until n do begin
if s( p ) then begin
if s( pos2 ) then begin % sexy pair %
addPrime( p, pairs, 5 );
if s( pos3 ) then begin % sexy triplet %
addPrime( p, triplets, 5 );
if s( pos4 ) then begin % sexy quadruplet %
addPrime( p, quadruplets, 5 );
if s( pos5 ) then begin % sexy quintuplet %
addPrime( p, quintuplets, 5 )
end if_s_pos5
end if_s_pos4
end if_s_pos3
end if_s_pos2
end if_s_p ;
pos2 := pos2 + 2;
pos3 := pos3 + 2;
pos4 := pos4 + 2;
pos5 := pos5 + 2
end for_p
end countSexyPrimes ;
% counts the number of unsexy primes up to n %
% the count is returned in the 0 element of the array %
% the last 5 ( or less if there are less than 5 ) unsexy prime is %
% returned in the array elements 1 to 10 %
procedure countUnsexyPrimes ( logical array s ( * )
; integer value n
; integer array unsexy ( * )
) ;
begin
for i := 0 until 10 do unsexy( i ) := 0;
for p := 2, 3, 5 do begin % handle primes below 7 separately %
if s( p ) and not s( p + 6 ) then addPrime( p, unsexy, 10 )
end for_p ;
for p := 7 step 2 until n do begin
if s( p ) and not s( p - 6 ) and not s( p + 6 ) then addPrime( p, unsexy, 10 )
end for_p
end countUnsexyPrimes ;
% shows sexy prime pairs %
procedure showPrimes ( integer value elements
; integer array primes ( * )
; integer value arrayMax
; string(24) value title
; integer value maxPrime
) ;
begin
write( i_w := 8, s_w := 0, "Found ", primes( 0 ), " ", title, " below ", maxPrime + 1
, i_w := 2, "; last ", ( if primes( 0 ) > arrayMax then arrayMax else primes( 0 ) ), ":"
);
write( i_w := 1, s_w := 0, " " );
for p := 1 until arrayMax do begin
if primes( p ) not = 0 then begin
integer pn;
if elements > 1 then writeon( "(" );
pn := primes( p ) - ( ( elements - 1 ) * 6 );
for i := 1 until elements do begin
writeon( i_w := 1, s_w := 0, " ", pn );
pn := pn + 6
end for_i ;
if elements > 1 then writeon( " ) " );
end if_primes_p_ne_0
end for_p
end showPrimes ;
integer MAX_SEXY, MAX_PRIME;
% for the task, we need to consider primes up to 1 000 035 %
% however we must still recognise sexy primes up that limit, so we sieve %
% up to 1 000 035 + 6 %
MAX_SEXY := 1000000 + 35;
MAX_PRIME := MAX_SEXY + 6;
begin
logical array s ( 1 :: MAX_PRIME );
integer array pairs, triplets, quadruplets, quintuplets ( 0 :: 5 );
integer array unsexy ( 0 :: 10 );
sieve( s, MAX_PRIME );
countSexyPrimes( s, MAX_SEXY, pairs, triplets, quadruplets, quintuplets );
countUnsexyPrimes( s, MAX_SEXY, unsexy );
showPrimes( 2, pairs, 5, "sexy prime pairs", MAX_SEXY );
showPrimes( 3, triplets, 5, "sexy prime triplets", MAX_SEXY );
showPrimes( 4, quadruplets, 5, "sexy prime quadruplets", MAX_SEXY );
showPrimes( 5, quintuplets, 5, "sexy prime quintuplets", MAX_SEXY );
showPrimes( 1, unsexy, 10, "unsexy primes", MAX_SEXY )
end
end.</syntaxhighlight>
{{out}}
<pre>
Found 16386 sexy prime pairs below 1000036; last 5:
( 999371 999377 ) ( 999431 999437 ) ( 999721 999727 ) ( 999763 999769 ) ( 999953 999959 )
Found 2900 sexy prime triplets below 1000036; last 5:
( 997427 997433 997439 ) ( 997541 997547 997553 ) ( 998071 998077 998083 ) ( 998617 998623 998629 ) ( 998737 998743 998749 )
Found 325 sexy prime quadruplets below 1000036; last 5:
( 977351 977357 977363 977369 ) ( 983771 983777 983783 983789 ) ( 986131 986137 986143 986149 ) ( 990371 990377 990383 990389 ) ( 997091 997097 997103 997109 )
Found 1 sexy prime quintuplets below 1000036; last 1:
( 5 11 17 23 29 )
Found 48627 unsexy primes below 1000036; last 10:
999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003
</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SEXY_PRIMES.AWK
BEGIN {
Line 86 ⟶ 323:
s[key] = str
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 106 ⟶ 343:
5 11 17 23 29,
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">include "isprime.kbs"
 
maxi = 1000035
cu = 0
c2 = 0
c3 = 0
c4 = 0
c5 = 0
#, n, i,
p = 0
dim unsexy(10)
dim pairs(5)
dim trips(5)
dim quads(5)
dim quins(5)
 
for n = maxi to 2 step -1
if isPrime(n) then
p += 1
if not isPrime(n-6) and not isPrime(n+6) then
if cu < 10 then unsexy[cu] = n
cu += 1
end if
if isPrime(n-6) then
if c2 < 5 then pairs[c2] = n
c2 += 1
if isPrime(n-12) then
if c3 < 5 then trips[c3] = n
c3 += 1
if isPrime(n-18) then
if c4 < 5 then quads[c4] = n
c4 += 1
if isPrime(n-24) then
if c5 < 5 then quins[c5] = n
c5 += 1
end if
end if
end if
end if
end if
next n
 
print p; " primes less than "; maxi
 
print chr(10); c2; " pairs ending with:"
for i = 4 to 0 step -1
print " ["; pairs[i]-6; ", "; pairs[i]; "]"
next i
 
print chr(10); c3; " triplets ending with:"
for i = 4 to 0 step -1
print " ["; trips[i]-12; ", "; trips[i]-6; ", "& trips[i]; "]"
next i
 
print chr(10); c4; " quadruplets ending with:"
for i = 4 to 0 step -1
print " ["; quads[i]-18; ", "; quads[i]-12; ", "; quads[i]-6; ", "; quads[i]; "]"
next i
 
print chr(10); c5; " quintuplet(s) ending with:"
if c5 > 5 then i = 5 else i = c5
for i = i-1 to 0 step -1
print " ["; quins[i]-24; ", "& quins[i]-18; ", "& quins[i]-12; ", "& quins[i]-6; ", "& quins[i]; "]"
next i
 
print chr(10); cu; " unsexy primes ending with:"
for i = 9 to 0 step -1
print unsexy[i]; ",";
next i
end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="vbnet">#include "isprime.bas"
 
#define maxi 1000035
Dim As Integer CU = 0, C2 = 0, C3 = 0, C4 = 0, C5 = 0, N, I, P = 0
Dim As Integer Unsexy(10), Pairs(5), Trips(5), Quads(5), Quins(5)
 
For N = maxi To 2 Step -1
If isPrime(N) Then
P += 1
If Not isPrime(N-6) And Not isPrime(N+6) Then
If CU < 10 Then Unsexy(CU) = N
CU += 1
End If
If isPrime(N-6) Then
If C2 < 5 Then Pairs(C2) = N
C2 += 1
If isPrime(N-12) Then
If C3 < 5 Then Trips(C3) = N
C3 += 1
If isPrime(N-18) Then
If C4 < 5 Then Quads(C4) = N
C4 += 1
If isPrime(N-24) Then
If C5 < 5 Then Quins(C5) = N
C5 += 1
End If
End If
End If
End If
End If
Next N
 
Print P; " primes less than"; maxi
 
Print Chr(10); C2; " pairs ending with:"
For I = 4 To 0 Step -1
Print " [" & Pairs(I)-6 & ", "& Pairs(I) & "]"
Next I
 
Print Chr(10); C3; " triplets ending with:"
For I = 4 To 0 Step -1
Print " [" & Trips(I)-12 & ", "& Trips(I)-6 & ", "& Trips(I) & "]"
Next I
 
Print Chr(10); C4; " quadruplets ending with:"
For I = 4 To 0 Step -1
Print " [" & Quads(I)-18 & ", "& Quads(I)-12 & ", "& Quads(I)-6 & ", "& Quads(I) & "]"
Next I
 
Print Chr(10); C5; " quintuplet(s) ending with:"
I = Iif(C5 > 5, 5, C5)
For I = I-1 To 0 Step -1
Print " [" & Quins(I)-24 & ", "& Quins(I)-18 & ", "& Quins(I)-12 & ", "& Quins(I)-6 & ", "& Quins(I) & "]"
Next I
 
Print Chr(10); CU; " unsexy primes ending with:"
For I = 9 To 0 Step -1
Print Unsexy(I); ",";
Next I
Print Chr(8); " "
Sleep</syntaxhighlight>
{{out}}
<pre> 78500 primes less than 1000035
 
16386 pairs ending with:
[999371, 999377]
[999431, 999437]
[999721, 999727]
[999763, 999769]
[999953, 999959]
 
2900 triplets ending with:
[997427, 997433, 997439]
[997541, 997547, 997553]
[998071, 998077, 998083]
[998617, 998623, 998629]
[998737, 998743, 998749]
 
325 quadruplets ending with:
[977351, 977357, 977363, 977369]
[983771, 983777, 983783, 983789]
[986131, 986137, 986143, 986149]
[990371, 990377, 990383, 990389]
[997091, 997097, 997103, 997109]
 
1 quintuplet(s) ending with:
[5, 11, 17, 23, 29]
 
48627 unsexy primes ending with:
999853, 999863, 999883, 999907, 999917, 999931, 999961, 999979, 999983, 1000003</pre>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">import isprime
 
maxi = 1000035
cu = 0
c2 = 0
c3 = 0
c4 = 0
c5 = 0
p = 0
dim unsexy(10), pairs(5), trips(5), quads(5), quins(5)
 
for n = maxi to 2 step -1
if isPrime(n) then
p = p + 1
if not isPrime(n - 6) and not isPrime(n + 6) then
if cu < 10 unsexy(cu) = n
cu = cu + 1
fi
if isPrime(n - 6) then
if c2 < 5 pairs(c2) = n
c2 = c2 + 1
if isPrime(n - 12) then
if c3 < 5 trips(c3) = n
c3 = c3 + 1
if isPrime(n - 18) then
if c4 < 5 quads(c4) = n
c4 = c4 + 1
if isPrime(n - 24) then
if c5 < 5 quins(c5) = n
c5 = c5 + 1
fi
fi
fi
fi
fi
next n
 
print p, " primes less than ", maxi
 
print chr$(10), c2, " pairs ending with:"
for i = 4 to 0 step -1
print " [", pairs(i)-6, ", ", pairs(i), "]"
next i
 
print chr$(10), c3, " triplets ending with:"
for i = 4 to 0 step -1
print " [", trips(i)-12, ", ", trips(i)-6, ", ", trips(i), "]"
next i
 
print chr$(10), c4, " quadruplets ending with:"
for i = 4 to 0 step -1
print " [", quads(i)-18, ", ", quads(i)-12, ", ", quads(i)-6, ", ", quads(i), "]"
next i
 
print chr$(10), c5, " quintuplet(s) ending with:"
if c5 > 5 then i = 5 else i = c5 : fi
for i = i-1 to 0 step -1
print " [", quins(i)-24, ", ", quins(i)-18, ", ", quins(i)-12, ", ", quins(i)-6, ", ", quins(i), "]"
next i
 
print chr$(10), cu, " unsexy primes ending with:"
for i = 9 to 0 step -1
print unsexy(i), ",";
next i
print chr$(8)," "
end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|C}}==
Similar approach to the Go entry but only stores the arrays that need to be printed out.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 279 ⟶ 756:
free(sv);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 306 ⟶ 783:
=={{header|C++}}==
{{libheader|Boost}}
<langsyntaxhighlight lang="cpp">#include <array>
#include <iostream>
#include <vector>
Line 376 ⟶ 853:
cout << '\n';
return 0;
}</langsyntaxhighlight>
 
Contents of prime_sieve.hpp:
<langsyntaxhighlight lang="cpp">#ifndef PRIME_SIEVE_HPP
#define PRIME_SIEVE_HPP
 
Line 430 ⟶ 907:
}
 
#endif</langsyntaxhighlight>
 
{{out}}
Line 448 ⟶ 925:
number of unsexy primes is 48627
last 10 unsexy primes: 999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003
</pre>
 
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Sexy_primes#Pascal Pascal].
 
=={{header|EasyLang}}==
<syntaxhighlight>
len isdiv[] 1000035
proc sieve . .
max = sqrt len isdiv[]
for d = 2 to max
if isdiv[d] = 0
for i = d * d step d to len isdiv[]
isdiv[i] = 1
.
.
.
.
sieve
#
proc showsx nr . .
for i = len isdiv[] - 6 * nr downto 3
if isdiv[i] = 0
h = 0
for j to nr
h += isdiv[i + j * 6]
.
if h = 0
cnt += 1
if cnt <= 5
s[] &= i
.
.
.
.
print cnt & " sexy primes of " & nr + 1
if cnt > 5
write "... "
.
for i = lower 5 len s[] downto 1
write "(" & s[i]
for j to nr
write " " & s[i] + j * 6
.
write ") "
.
print ""
.
proc showunsx . .
for i = len isdiv[] - 6 downto 2
if isdiv[i] = 0 and isdiv[i + 6] = 1 and (i <= 6 or isdiv[i - 6] = 1)
cnt += 1
if cnt <= 10
s[] &= i
.
.
.
print cnt & " unsexy primes"
write "... "
for i = 10 downto 1
write s[i] & " "
.
print ""
.
showsx 1
showsx 2
showsx 3
showsx 4
showunsx
</syntaxhighlight>
 
{{out}}
<pre>
16386 sexy primes of 2
... (999371 999377) (999431 999437) (999721 999727) (999763 999769) (999953 999959)
2900 sexy primes of 3
... (997427 997433 997439) (997541 997547 997553) (998071 998077 998083) (998617 998623 998629) (998737 998743 998749)
325 sexy primes of 4
... (977351 977357 977363 977369) (983771 983777 983783 983789) (986131 986137 986143 986149) (990371 990377 990383 990389) (997091 997097 997103 997109)
1 sexy primes of 5
(5 11 17 23 29)
48627 unsexy primes
... 999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003
</pre>
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Sexy primes. Nigel Galloway: October 2nd., 2018
let n=pCache |> Seq.takeWhile(fun n->n<1000035) |> Seq.filter(fun n->(not (isPrime(n+6)) && (not isPrime(n-6))))) |> Array.ofSeq
Line 469 ⟶ 1,029:
printfn "There are %d sexy prime quintuplets all components of which are less than 1,000,035. The last 5 are:" nigel.Length
Array.skip (nigel.Length-5) nigel |> Array.iter(fun n->printf "(%d,%d,%d,%d,%d) " (n-24) (n-18) (n-12) (n-6) n); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 485 ⟶ 1,045:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: combinators.short-circuit fry interpolate io kernel
literals locals make math math.primes math.ranges prettyprint qw
sequences tools.memory.private ;
Line 528 ⟶ 1,088:
: main ( -- ) 2 5 [a,b] [ show-tuplets ] each show-unsexy ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 548 ⟶ 1,108:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 660 ⟶ 1,220:
le, n, verb = printHelper("unsexy primes", len(unsexy), lim, 10)
fmt.Printf("The last %d %s:\n %v\n\n", n, verb, unsexy[le-n:])
}</langsyntaxhighlight>
 
{{out}}
Line 686 ⟶ 1,246:
=={{header|Haskell}}==
Uses Library primes. https://hackage.haskell.org/package/primes (wheel sieve).
<syntaxhighlight lang="haskell">import Text.Printf (printf)
<lang haskell>
import Text.Printf (printf)
import Data.Numbers.Primes (isPrime, primes)
 
maintype ::Pair IO= (Int, Int)
type Triplet = (Int, Int, Int)
main = do
type Quad = (Int, Int, Int, Int)
printf ("Number of sexy prime pairs: %d\n" ++ lastFiveText) (length pairs) (lastFive pairs)
type Quin = (Int, Int, Int, Int, Int)
printf ("Number of sexy prime triplets: %d\n" ++ lastFiveText) (length triplets) (lastFive triplets)
printf ("Number of sexy prime quadruplets: %d\n" ++ lastFiveText) (length quads) (lastFive quads)
printf "Number of sexy prime quintuplets: %d\n Last 1 : %s\n\n" (length quins) (show $ drop (length quins -1) quins)
printf "Number of unsexy primes: %d\n Last 10: %s\n\n" (length unsexy) (show $ drop (length unsexy - 10) unsexy)
where d = takeWhile (< 1000035) primes
unsexy = filter (\n -> (not $ isPrime (n+6)) && (not $ isPrime (n-6))) d
pairs = foldr (\a b -> if isPrime (a-6) then (a-6,a) : b else b) [] d
triplets = foldr (\a b -> if isPrime (a-12) && isPrime (a-6) then (a-12,a-6,a) : b else b) [] d
quads = foldr (\a b -> if isPrime (a-18) && isPrime (a-12) && isPrime (a-6) then (a-18,a-12,a-6,a) : b else b) [] d
quins = foldr (\a b -> if isPrime (a-24) && isPrime (a-18) && isPrime (a-12) && isPrime (a-6) then (a-24, a-18,a-12,a-6,a) : b else b) [] d
lastFive xs = show $ drop (length xs - 5) xs
lastFiveText = " Last 5 : %s\n\n"
</lang>
Or using a single fold for increased efficiency
<lang haskell>
import Text.Printf (printf)
import Data.Numbers.Primes (isPrime, primes)
 
datatype Result = Result ([(IntPair], Int)[Triplet], [(IntQuad], Int, Int)] [(Int, Int, Int, Int)Quin] [(Int, Int, Int, Int, Int)] [Int])
 
groups :: Int -> Result -> Result
groups n r@(Result p, t, q, qn, u)
| isPrime (n-24)n4 && isPrime (n-18)n3 && isPrime (n-12)n2 && isPrime (n-6)n1 = Result(addPair, asPairaddTriplet, asTripletaddQuad, asQuad asQuinaddQuin, u)
| isPrime (n-18)n3 && isPrime (n-12)n2 && isPrime (n-6)n1 = Result asPair asTriplet asQuad = (addPair, addTriplet, addQuad, qn, u)
| isPrime (n-12)n2 && isPrime (n-6)n1 = Result asPair asTriplet = (addPair, addTriplet, q, qn, u)
| isPrime (n-6)n1 = Result asPair = (addPair, t, q, qn, u)
| (not $ (isPrime (n+6)) && (not $ (isPrime (n-6)n1) = Result = (p, t, q, qn, (n : u)
| otherwise = r
where asPairaddPair = (n-6n1, n) : p
asTripletaddTriplet = (n-12n2, n-6n1, n) : t
asQuadaddQuad = (n-18n3, n-12n2, n-6n1, n) : q
asQuinaddQuin = (n-24n4, n-18n3, n-12n2, n-6n1, n) : qn
n1 = n - 6
n2 = n - 12
n3 = n - 18
n4 = n - 24
 
main :: IO ()
main = do
printf ("Number of sexy prime pairs: %d\n" ++<> lastFiveText) (length pairs) (lastFive pairs)
printf ("Number of sexy prime triplets: %d\n" ++<> lastFiveText) (length triplets) (lastFive triplets)
printf ("Number of sexy prime quadruplets: %d\n" ++<> lastFiveText) (length quads) (lastFive quads)
printf "Number of sexy prime quintuplets: %d\n Last 1 : %s\n\n" (length quins) (show $ drop (length quins -1)last quins)
printf "Number of unsexy primes: %d\n Last 10: %s\n\n" (length unsexy) (show $ drop (length unsexy - 10) unsexy)
where (Result pairs, triplets, quads, quins, unsexy) = foldr groups (Result [], [], [], [], []) $ takeWhile (< 1000035) primes
lastFive xs = show $ drop (length xs - 5) xs
lastFiveText = " Last 5 : %s\n\n"</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 754 ⟶ 1,300:
Last 10: [999853,999863,999883,999907,999917,999931,999961,999979,999983,1000003]
</pre>
Slight variation which only holds on to the display results. Does not perform any better than above though. Both run ~ 250ms.
<syntaxhighlight lang="haskell">{-# LANGUAGE TemplateHaskell #-}
import Control.Lens (makeLenses, over, (^.), to, view)
import Data.Numbers.Primes (isPrime, primes)
import Text.Printf (printf)
 
data Group a = Group { _count :: Int, _results :: [a] } deriving (Show)
makeLenses ''Group
 
type Groups = ( Group (Int, Int)
, Group (Int, Int, Int)
, Group (Int, Int, Int, Int)
, Group (Int, Int, Int, Int, Int)
, Group Int )
 
initialGroups :: Groups
initialGroups = let newGroup = Group 0 []
in (newGroup, newGroup, newGroup, newGroup, newGroup)
 
collect :: Groups -> Int -> Groups
collect r@(pr, tt, qd, qn, un) n
| isPrime n4 && isPrime n3 && isPrime n2 && isPrime n1 = (addPair pr, addTriplet tt, addQuad qd, addQuin qn, un)
| isPrime n3 && isPrime n2 && isPrime n1 = (addPair pr, addTriplet tt, addQuad qd, qn, un)
| isPrime n2 && isPrime n1 = (addPair pr, addTriplet tt, qd, qn, un)
| isPrime n1 = (addPair pr, tt, qd, qn, un)
| not (isPrime (n+6)) && not (isPrime n1) = (pr, tt, qd, qn, addUnSexy un)
| otherwise = r
where
n1 = n-6
n2 = n-12
n3 = n-18
n4 = n-24
 
addPair = over count succ . over results (take 5 . (:) (n1, n))
addTriplet = over count succ . over results (take 5 . (:) (n2, n1, n))
addQuad = over count succ . over results (take 5 . (:) (n3, n2, n1, n))
addQuin = over count succ . over results (take 1 . (:) (n4, n3, n2, n1, n))
addUnSexy = over count succ . over results (take 10 . (:) n)
 
main :: IO ()
main = do
let (pr, tt, qd, qn, un) = collectGroups primes
printf "Number of sexy prime pairs: %d\n Last 5 : %s\n\n" (pr ^. count) (pr ^. results . to display)
printf "Number of sexy prime triplets: %d\n Last 5 : %s\n\n" (tt ^. count) (tt ^. results . to display)
printf "Number of sexy prime quadruplets: %d\n Last 5 : %s\n\n" (qd ^. count) (qd ^. results . to display)
printf "Number of sexy prime quintuplets: %d\n Last 1 : %s\n\n" (qn ^. count) (qn ^. results . to display)
printf "Number of unsexy primes: %d\n Last 10: %s\n\n" (un ^. count) (un ^. results . to display)
where
collectGroups = foldl collect initialGroups . takeWhile (< 1000035)
display :: Show a => [a] -> String
display = show . reverse</syntaxhighlight>
 
=={{header|J}}==
A brute force approach finds primes in the task range which are preceded by primes with appropriate offsets:
<syntaxhighlight lang=J> #sp2=: (#~ 1 p: _6+]) p1=:i.&.(p:inv) 1000035 NB. pairs
16386
(_6*i.-2)+/_5{.sp2
999371 999431 999721 999763 999953
999377 999437 999727 999769 999959
#sp3=: (#~ 1 p: _12+]) sp2 NB. triplets
2900
(_6*i.-3)+/_5{.sp3
997427 997541 998071 998617 998737
997433 997547 998077 998623 998743
997439 997553 998083 998629 998749
#sp4=: (#~ 1 p: _18+]) sp3 NB. quads
325
(_6*i.-5)+/_5{.sp4
977345 983765 986125 990365 997085
977351 983771 986131 990371 997091
977357 983777 986137 990377 997097
977363 983783 986143 990383 997103
977369 983789 986149 990389 997109
#sp5=: (#~ 1 p: _24+]) sp4 NB. quint
1
(_6*i.5)+/sp5
29
23
17
11
5
#unp=: p1-. 0 6+/(#~ 1 p: 6+]) p1
48627
_10{.unp
999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003
</syntaxhighlight>
 
And here's a different approach:
<syntaxhighlight lang="j">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 =: ] #~ + e. ]
 
NB. Iterate "sexy" logic up to orgy size
orgy =: sexy&.>^:( ({.@:[) ` (<@:{:@:[) ` (<@:]) )
 
sp =: dyad define
'pd os' =. x NB. x is prime distance (6), orgy size (5)
p =. pngt y
o =. x orgy p
g =. o +/&.> <\ +/\ _1 |.!.0 os # pd NB. Groups
 
's g' =. split g NB. Split singles from groups
l =. (({.~ -) 5 <. #)&.> g NB. Last (max) 5 groups
 
NB. I'm sure there's something clever with p-.s or similar,
NB. but (a) I don't want to think through it, and (b)
NB. it causes the kind of edge-case issues the spec warns
NB. about with 1000033
us =. p (] #~ 1 +:/@:p: +/)~ (+,-) pd NB. Unsexy numbers
( (# ; _10&{.) us ) , (#&.> g) ,. l
)</syntaxhighlight>
 
{{out}}
<syntaxhighlight lang="j"> r =: 6 5 sp 1000035 NB. 6=sex=prime distance, 5=max orgy size
(;:'Group Count Examples') , (;:'Unsexy Pairs Triplets Quadruplets Quintuplets') ,. r
+-----------+-----+----------------------------------------------------------------------+
|Group |Count|Examples |
+-----------+-----+----------------------------------------------------------------------+
|Unsexy |48627|999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003|
+-----------+-----+----------------------------------------------------------------------+
|Pairs |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 |
+-----------+-----+----------------------------------------------------------------------+</syntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.List;
Line 830 ⟶ 1,520:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 849 ⟶ 1,539:
The last 10 unsexy primes:
[999853], [999863], [999883], [999907], [999917], [999931], [999961], [999979], [999983], [1000003]
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
''Also works with gojq and fq''
 
See e.g. [[Anaprimes#jq]] for a suitable jq def of `primeSieve`.
<syntaxhighlight lang=jq>
include "primeSieve"; # or copy-and-paste its def
 
def when(filter; action): if filter // null then action else . end;
 
def results($cat; $lim; $max; $array):
($array|length) as $len
| (if $cat != "unsexy primes" then "sexy prime " + $cat else $cat end) as $cat
| (if $len < $max then $len else $max end) as $last
| (if $last == 1 then "is" else "are" end) as $verb
| "Number of \($cat) less than \($lim) = \($len)",
"The last \($max) \($verb):\n \($array[ - $last :])\n";
 
def task($lim):
(($lim-1) | primeSieve) as $sieve # $sieve[i] iff i is prime
| { pairs:[], trips:[], quads:[], quins:[], unsexy:[2, 3], i: 3 }
| until (.i >= $lim;
if .i > 5 and .i < $lim-6 and $sieve[.i] and ($sieve[.i-6]|not) and ($sieve[.i+6]|not)
then .unsexy += [.i]
else when(.i < $lim-6 and $sieve[.i] and $sieve[.i+6];
.pairs += [[.i, .i+6]]
| when(.i < $lim-12 and $sieve[.i+12];
.trips += [[.i, .i+6, .i+12]]
| when(.i < $lim-18 and $sieve[.i+18];
.quads += [[.i, .i+6, .i+12, .i+18]]
| when(.i < $lim-24 and $sieve[.i+24];
.quins += [[.i, .i+6, .i+12, .i+18, .i+24]]))))
end
| .i += 2 )
| results("pairs"; $lim; 5; .pairs),
results("triplets"; $lim; 5; .trips),
results("quadruplets"; $lim; 5; .quads),
results("quintuplets"; $lim; 5; .quins),
results("unsexy primes"; $lim; 10; .unsexy)
;
 
task(1000035)
</syntaxhighlight>
{{output}}
<pre>
Number of sexy prime pairs less than 1000035 = 16386
The last 5 are:
[[999371,999377],[999431,999437],[999721,999727],[999763,999769],[999953,999959]]
 
Number of sexy prime triplets less than 1000035 = 2900
The last 5 are:
[[997427,997433,997439],[997541,997547,997553],[998071,998077,998083],[998617,998623,998629],[998737,998743,998749]]
 
Number of sexy prime quadruplets less than 1000035 = 325
The last 5 are:
[[977351,977357,977363,977369],[983771,983777,983783,983789],[986131,986137,986143,986149],[990371,990377,990383,990389],[997091,997097,997103,997109]]
 
Number of sexy prime quintuplets less than 1000035 = 1
The last 5 is:
[[5,11,17,23,29]]
 
Number of unsexy primes less than 1000035 = 48627
The last 10 are:
[999853,999863,999883,999907,999917,999931,999961,999979,999983,1000003]
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
using Primes
 
Line 928 ⟶ 1,685:
end
 
primesbysexiness(1000035) </langsyntaxhighlight> {{output}} <pre>
There are:
16386 twins,
Line 944 ⟶ 1,701:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">// Version 1.2.71
 
fun sieve(lim: Int): BooleanArray {
Line 1,023 ⟶ 1,780:
var (nu, verbu) = printHelper("unsexy primes", unsexy.size, lim, 10)
System.out.printf("The last %d %s:\n %s\n\n", nu, verbu, unsexy.takeLast(nu))
}</langsyntaxhighlight>
 
{{output}}
Line 1,050 ⟶ 1,807:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">local N = 1000035
 
-- FUNCS:
Line 1,073 ⟶ 1,830:
end
local unsexy = primes:filter(function(v) return not (v>=N or sieve[v-6] or sieve[v+6]) end)
print(#unsexy .. " unsexy, ending with: " ..unsexy:lastn(10):concat(" "))</langsyntaxhighlight>
{{out}}
<pre>16386 pairs, ending with: (999371 999377) (999431 999437) (999721 999727) (999763 999769) (999953 999959)
Line 1,080 ⟶ 1,837:
1 quintuplets, ending with: (5 11 17 23 29)
48627 unsexy, ending with: 999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[AllSublengths]
AllSublengths[l_List] := If[Length[l] > 2,
Catenate[Partition[l, #, 1] & /@ Range[2, Length[l]]]
,
{l}
]
primes = Prime[Range[PrimePi[1000035]]];
ps = Union[Intersection[primes + 6, primes] - 6, Intersection[primes - 6, primes] + 6];
a = Intersection[ps + 6, ps] - 6;
b = Intersection[ps - 6, ps] + 6;
g = Graph[DeleteDuplicates[Thread[a \[UndirectedEdge] (a + 6)]~Join~Thread[(b - 6) \[UndirectedEdge] b]]];
sp = Sort /@ ConnectedComponents[g];
sp //= SortBy[First];
sp //= Map[AllSublengths];
sp //= Catenate;
sp //= SortBy[First];
sp //= DeleteDuplicates;
sel = Select[sp, Length /* EqualTo[2]];
Length[sel]
sel[[-5 ;;]] // Column
sel = Select[sp, Length /* EqualTo[3]];
Length[sel]
sel[[-5 ;;]] // Column
sel = Select[sp, Length /* EqualTo[4]];
Length[sel]
sel[[-5 ;;]] // Column
sel = Select[sp, Length /* EqualTo[5]];
Length[sel]
sel // Column
 
Select[Complement[primes, DeleteDuplicates[Catenate@sp]][[-20 ;;]], ! (PrimeQ[# + 6] \[Or] PrimeQ[# - 6]) &][[-10 ;;]] // Column</syntaxhighlight>
{{out}}
<pre>16386
{999371,999377}
{999431,999437}
{999721,999727}
{999763,999769}
{999953,999959}
2900
{997427,997433,997439}
{997541,997547,997553}
{998071,998077,998083}
{998617,998623,998629}
{998737,998743,998749}
325
{977351,977357,977363,977369}
{983771,983777,983783,983789}
{986131,986137,986143,986149}
{990371,990377,990383,990389}
{997091,997097,997103,997109}
1
{5,11,17,23,29}
999853
999863
999883
999907
999917
999931
999961
999979
999983
1000003</pre>
 
=={{header|Nim}}==
{{trans|Kotlin}}
This Nim version uses Kotlin algorithm with several differences. In particular, we have chosen to store only the first term of groups as others can be retrieved by computation. But it complicates somewhat the printing of results.
 
<syntaxhighlight lang="nim">import math, strformat, strutils
 
const Lim = 1_000_035
 
type Group {.pure.} = enum # "ord" gives the number of terms.
Unsexy = (1, "unsexy primes")
Pairs = (2, "sexy prime pairs")
Triplets = (3, "sexy prime triplets")
Quadruplets = (4, "sexy prime quadruplets")
Quintuplets = (5, "sexy prime quintuplets")
 
 
# Sieve of Erathosthenes.
var composite: array[1..Lim, bool] # Default is false.
composite[1] = true
for p in countup(3, sqrt(Lim.toFloat).int, 2): # Ignore even numbers.
if not composite[p]:
for k in countup(p * p, Lim, 2 * p):
composite[k] = true
 
template isPrime(n: int): bool = not composite[n]
 
 
proc expandGroup(n: int; group: Group): string =
## Given the first term of a group, return the full group
## representation as a string.
var n = n
for _ in 1..ord(group):
result.addSep(", ")
result.add $n
inc n, 6
if group != Unsexy: result = '(' & result & ')'
 
 
proc printResult(group: Group; values: seq[int]; count: int) =
## Print a result.
 
echo &"\nNumber of {group} less than {Lim}: {values.len}"
let last = min(values.len, count)
let verb = if last == 1: "is" else: "are"
echo &"The last {last} {verb}:"
 
var line = ""
for i in countdown(last, 1):
line.addSep(", ")
line.add expandGroup(values[^i], group)
echo " ", line
 
 
var
pairs, trips, quads, quints: seq[int] # Keep only the first prime of the group.
unsexy = @[2, 3]
 
for n in countup(3, Lim, 2):
if composite[n]: continue
 
if n in 7..(Lim - 8) and composite[n - 6] and composite[n + 6]:
unsexy.add n
continue
 
if n < Lim - 6 and isPrime(n + 6):
pairs.add n
else: continue
 
if n < Lim - 12 and isPrime(n + 12):
trips.add n
else: continue
 
if n < Lim - 18 and isPrime(n + 18):
quads.add n
else: continue
 
if n < Lim - 24 and isPrime(n + 24):
quints.add n
 
printResult(Pairs, pairs, 5)
printResult(Triplets, trips, 5)
printResult(Quadruplets, quads, 5)
printResult(Quintuplets, quints, 5)
printResult(Unsexy, unsexy, 10)</syntaxhighlight>
 
{{out}}
<pre>Number of sexy prime pairs less than 1000035: 16386
The last 5 are:
(999371, 999377), (999431, 999437), (999721, 999727), (999763, 999769), (999953, 999959)
 
Number of sexy prime triplets less than 1000035: 2900
The last 5 are:
(997427, 997433, 997439), (997541, 997547, 997553), (998071, 998077, 998083), (998617, 998623, 998629), (998737, 998743, 998749)
 
Number of sexy prime quadruplets less than 1000035: 325
The last 5 are:
(977351, 977357, 977363, 977369), (983771, 983777, 983783, 983789), (986131, 986137, 986143, 986149), (990371, 990377, 990383, 990389), (997091, 997097, 997103, 997109)
 
Number of sexy prime quintuplets less than 1000035: 1
The last 1 is:
(5, 11, 17, 23, 29)
 
Number of unsexy primes less than 1000035: 48627
The last 10 are:
999853, 999863, 999883, 999907, 999917, 999931, 999961, 999979, 999983, 1000003</pre>
 
=={{header|Pascal}}==
Line 1,089 ⟶ 2,016:
37907606 unsexy primes // = 50847538-2*6849047+758163-1
It seems so, not a proove.
<langsyntaxhighlight lang="pascal">program SexyPrimes;
 
uses
SysUtils;
{$IFNDEF FPC}
,windows // GettickCount64
{$ENDIF}
 
const
Line 1,265 ⟶ 2,195:
writeln(unsexyprimes,' unsexy primes');
OutLastUnsexy(10);
end.</langsyntaxhighlight>
{{Output}}
<pre>
Line 1,308 ⟶ 2,238:
{{libheader|ntheory}}
We will use the prime iterator and primality test from the <code>ntheory</code> module.
<langsyntaxhighlight lang="perl">use ntheory qw/prime_iterator is_prime/;
 
sub tuple_tail {
Line 1,354 ⟶ 2,284:
 
print "Number of unsexy primes less than $cmax: ". comma(scalar @{$primes{unsexy}}) . "\n";
print " Last 10 unsexy primes less than $cmax: ". join(' ', @{$primes{unsexy}}[-10..-1]) . "\n";</langsyntaxhighlight>
{{out}}
<pre>Total primes less than 1,000,035: 78,500
Line 1,378 ⟶ 2,308:
The cluster sieve becomes more efficient as the number of terms increases. See for example [[oeis:a213646|OEIS Prime 11-tuplets]].
 
<langsyntaxhighlight lang="perl">use ntheory qw/sieve_prime_cluster forprimes is_prime/;
 
# ... identical helper functions
Line 1,395 ⟶ 2,325:
} $max-1;
 
# ... identical output code</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function create_sieve(integer limit)
<span style="color: #008080;">function</span> <span style="color: #000000;">create_sieve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span>
sequence sieve = repeat(true,limit)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sieve</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span>
sieve[1] = false
<span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
for i=4 to limit by 2 do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span> <span style="color: #008080;">to</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
sieve[i] = false
<span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for p=3 to floor(sqrt(limit)) by 2 do
<span style="color: #008080;">for</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
integer p2 = p*p
<span style="color: #004080;">integer</span> <span style="color: #000000;">p2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">*</span><span style="color: #000000;">p</span>
if sieve[p2] then
<span style="color: #008080;">if</span> <span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
for k=p2 to limit by p*2 do
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">p2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">by</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
sieve[k] = false
<span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return sieve
<span style="color: #008080;">return</span> <span style="color: #000000;">sieve</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
constant lim = 1000035,
<span style="color: #008080;">constant</span> <span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1000035</span><span style="color: #0000FF;">,</span>
--constant lim = 100, -- (this works too)
<span style="color: #000080;font-style:italic;">--constant lim = 100, -- (this works too)</span>
limit = lim-(and_bits(lim,1)=0), -- (limit must be odd)
<span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lim</span><span style="color: #0000FF;">-(</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- (limit must be odd)</span>
sieve = create_sieve(limit+6) -- (+6 to check for sexiness)
<span style="color: #000000;">sieve</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">create_sieve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">+</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (+6 to check for sexiness)</span>
 
sequence sets = repeat({},5), -- (unsexy,pairs,trips,quads,quins)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sets</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">({},</span><span style="color: #000000;">5</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- (unsexy,pairs,trips,quads,quins)</span>
limits = {10,5,4,3,1},
<span style="color: #000000;">limits</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
counts = 1&repeat(0,4) -- (2 is an unsexy prime)
<span style="color: #000000;">counts</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (2 is an unsexy prime)</span>
integer total = 1 -- ""
<span style="color: #004080;">integer</span> <span style="color: #000000;">total</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- ""</span>
 
for i=limit to 3 by -2 do -- (this loop skips 2)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">limit</span> <span style="color: #008080;">to</span> <span style="color: #000000;">3</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">2</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (this loop skips 2)</span>
if sieve[i] then
<span style="color: #008080;">if</span> <span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
total += 1
<span style="color: #000000;">total</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
if sieve[i+6]=false and (i-6<0 or sieve[i-6]=false) then
<span style="color: #008080;">if</span> <span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">6</span><span style="color: #0000FF;">]=</span><span style="color: #004600;">false</span> <span style="color: #008080;">and</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">6</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">6</span><span style="color: #0000FF;">]=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
counts[1] += 1 -- unsexy
<span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- unsexy</span>
if length(sets[1])<limits[1] then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])<</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
sets[1] = prepend(sets[1],i)
<span style="color: #000000;">sets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prepend</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
else
<span style="color: #008080;">else</span>
sequence set = {i}
<span style="color: #004080;">sequence</span> <span style="color: #000000;">set</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">}</span>
for j=i-6 to 3 by -6 do
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">6</span> <span style="color: #008080;">to</span> <span style="color: #000000;">3</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">6</span> <span style="color: #008080;">do</span>
if j<=0 or sieve[j]=false then exit end if
<span style="color: #008080;">if</span> <span style="color: #000000;">j</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]=</span><span style="color: #004600;">false</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
set = prepend(set,j)
<span style="color: #000000;">set</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prepend</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">)</span>
integer l = length(set)
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set</span><span style="color: #0000FF;">)</span>
if length(sets[l])<limits[l] then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">])<</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
sets[l] = prepend(sets[l],set)
<span style="color: #000000;">sets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prepend</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">],</span><span style="color: #000000;">set</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
counts[l] += 1
<span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
if length(sets[1])<limits[1] then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])<</span><span style="color: #000000;">limits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
sets[1] = prepend(sets[1],2) -- (as 2 skipped above)
<span style="color: #000000;">sets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prepend</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (as 2 skipped above)</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
constant fmt = """
<span style="color: #008080;">constant</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Of %,d primes less than %,d there are:
Of %,d unsexy primes, theless lastthan %,d beingthere %sare:
%,d pairsunsexy primes, the last %d being %s
%,d tripletspairs, the last %d being %s
%,d quadrupletstriplets, the last %d being %s
%,d quintupletquadruplets, the last %d being %s
%,d quintuplet, the last %d being %s
"""
"""</span>
sequence results = {total,lim,
<span style="color: #004080;">sequence</span> <span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">total</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span>
0,0,"",
<span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span>
0,0,"",
<span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span>
0,0,"",
<span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span>
0,0,"",
<span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span>
0,0,""}
<span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">}</span>
for i=1 to 5 do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">do</span>
results[i*3..i*3+2] = {counts[i],length(sets[i]),sprint(sets[i])}
<span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">*</span><span style="color: #000000;">3</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">*</span><span style="color: #000000;">3</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]),</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])}</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,fmt,results)</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">results</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,481 ⟶ 2,413:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">sexy_prime_group(1, N, _, [N]):-
is_prime(N),
!.
Line 1,527 ⟶ 2,459:
 
main:-
main(1000035).</langsyntaxhighlight>
 
Module for finding prime numbers up to some limit:
<langsyntaxhighlight lang="prolog">:- module(prime_numbers, [find_prime_numbers/1, is_prime/1]).
:- dynamic is_prime/1.
 
Line 1,571 ⟶ 2,503:
cross_out(S, N, P):-
Q is S + 2 * P,
cross_out(Q, N, P).</langsyntaxhighlight>
 
{{out}}
Line 1,589 ⟶ 2,521:
Number of unsexy primes is 48627
Last 10 unsexy primes: [999853,999863,999883,999907,999917,999931,999961,999979,999983,1000003]
</pre>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">DisableDebugger
EnableExplicit
 
#LIM=1000035
 
Macro six(mul)
6*mul
EndMacro
 
Macro form(n)
RSet(Str(n),8)
EndMacro
 
Macro put(m,g,n)
PrintN(Str(m)+" "+g)
PrintN(n)
EndMacro
 
Define c1.i=2,c2.i,c3.i,c4.i,c5.i,t1$,t2$,t3$,t4$,t5$,i.i,j.i
 
Global Dim soe.b(#LIM)
FillMemory(@soe(0),#LIM,#True,#PB_Byte)
If Not OpenConsole("")
End 1
EndIf
 
For i=2 To Sqr(#LIM)
If soe(i)=#True
j=i*i
While j<=#LIM
soe(j)=#False
j+i
Wend
EndIf
Next
 
Procedure.s formtab(t$,l.i)
If CountString(t$,~"\n")>l
t$=Mid(t$,FindString(t$,~"\n")+1)
EndIf
ProcedureReturn t$
EndProcedure
 
For i=3 To #LIM Step 2
If i>5 And i<#LIM-6 And soe(i)&~(soe(i-six(1))|soe(i+six(1)))
c1+1
t1$+form(i)+~"\n"
t1$=formtab(t1$,10)
Continue
EndIf
If i<#LIM-six(1) And soe(i)&soe(i+six(1))
c2+1
t2$+form(i)+form(i+six(1))+~"\n"
t2$=formtab(t2$,5)
EndIf
If i<#LIM-six(2) And soe(i)&soe(i+six(1))&soe(i+six(2))
c3+1
t3$+form(i)+form(i+six(1))+form(i+six(2))+~"\n"
t3$=formtab(t3$,5)
EndIf
If i<#LIM-six(3) And soe(i)&soe(i+six(1))&soe(i+six(2))&soe(i+six(3))
c4+1
t4$+form(i)+form(i+six(1))+form(i+six(2))+form(i+six(3))+~"\n"
t4$=formtab(t4$,5)
EndIf
If i<#LIM-six(4) And soe(i)&soe(i+six(1))&soe(i+six(2))&soe(i+six(3))&soe(i+six(4))
c5+1
t5$+form(i)+form(i+six(1))+form(i+six(2))+form(i+six(3))+form(i+six(4))+~"\n"
t5$=formtab(t5$,5)
EndIf
Next
 
put(c2,"pairs ending with ...",t2$)
put(c3,"triplets ending with ...",t3$)
put(c4,"quadruplets ending with ...",t4$)
put(c5,"quintuplets ending with ...",t5$)
put(c1,"unsexy primes ending with ...",t1$)
 
Input()</syntaxhighlight>
{{out}}
<pre>16386 pairs ending with ...
999371 999377
999431 999437
999721 999727
999763 999769
999953 999959
 
2900 triplets ending with ...
997427 997433 997439
997541 997547 997553
998071 998077 998083
998617 998623 998629
998737 998743 998749
 
325 quadruplets ending with ...
977351 977357 977363 977369
983771 983777 983783 983789
986131 986137 986143 986149
990371 990377 990383 990389
997091 997097 997103 997109
 
1 quintuplets ending with ...
5 11 17 23 29
 
48627 unsexy primes ending with ...
999853
999863
999883
999907
999917
999931
999961
999979
999983
1000003
</pre>
 
=={{header|Python}}==
===Imperative Style===
<langsyntaxhighlight lang="python">LIMIT = 1_000_035
def primes2(limit=LIMIT):
if limit < 2: return []
Line 1,648 ⟶ 2,698:
print(f'\nThere are {len(unsexy)} unsexy primes ending with ...')
for usx in unsexy[-10:]:
print(' ',usx)</langsyntaxhighlight>
 
{{out}}
Line 1,688 ⟶ 2,738:
{{trans|FSharp}}
This task uses [[Extensible_prime_generator#210-wheel_postponed_incremental_sieve]]
<langsyntaxhighlight lang="python">
#Functional Sexy Primes. Nigel Galloway: October 5th., 2018
from itertools import *
Line 1,709 ⟶ 2,759:
print ("There are",len(unsexy),"unsexy primes less than 1,000,035. The last 10 are:")
for g in islice(unsexy,max(len(unsexy)-10,0),len(unsexy)): print(g)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,743 ⟶ 2,793:
999983
1000003
</pre>
 
=={{header|Quackery}}==
 
<code>eratosthenes</code> and <code>primes</code> are defined at [[Sieve of Eratosthenes#Quackery]].
 
<syntaxhighlight lang="Quackery"> 1000035 eratosthenes
 
[ stack ] is pairs ( --> s )
[ stack ] is trips ( --> s )
[ stack ] is quads ( --> s )
[ stack ] is quins ( --> s )
[ stack ] is unsexy ( --> s )
 
[ share swap bit & 0 != ] is in ( s n --> b )
 
primes share dup
' [ pairs trips quads quins ]
witheach
[ dip [ dip dup 6 >> & dup ] put ]
2drop
 
pairs share
dup 6 << | ~
primes share &
unsexy put
 
' [ pairs trips quads quins ]
witheach
[ temp put
[] 1000035 times
[ i^ temp share in if
[ i^ join ] ]
dup size echo sp
-5 split echo drop cr
temp release ]
cr
[] 1000035 6 - times
[ i^ unsexy in if
[ i^ join ] ]
dup size echo sp
-10 split echo drop cr</syntaxhighlight>
 
{{out}}
 
<pre>16386 [ 999371 999431 999721 999763 999953 ]
2900 [ 997427 997541 998071 998617 998737 ]
325 [ 977351 983771 986131 990371 997091 ]
1 [ 5 ]
 
48627 [ 999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003 ]
</pre>
 
Line 1,749 ⟶ 2,850:
{{works with|Rakudo|2018.08}}
 
<syntaxhighlight lang="raku" perl6line>use Math::Primesieve;
my $sieve = Math::Primesieve.new;
 
Line 1,782 ⟶ 2,883:
}
 
sub comma { $^i.flip.comb(3).join(',').flip }</langsyntaxhighlight>
{{out}}
<pre>Total primes less than 1,000,035: 78,500
Line 1,801 ⟶ 2,902:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays various kinds of sexy and unsexy primes less than N.*/
parse arg N endU end2 end3 end4 end5 . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 1000035 - 1 /*Not specified? Then use the default.*/
Line 1,888 ⟶ 2,989:
q=p-18; if \x.q then iterate
v=p-24; if \x.v then iterate; x5=x5 v'~'q"~"t'~' || b"~"p
end /*k*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
 
Line 1,917 ⟶ 3,018:
 
=={{header|Ring}}==
{{Improve|Ring|Does not even ''attempt'' to fulfil the task requirements and has no explanation as to why not}}
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,928 ⟶ 3,030:
 
see "Sexy prime pairs under 100:" + nl + nl
for n = 1 to len(primes)-1
for m = n + 1 to len(primes)
if primes[m] - primes[n] = 6
Line 1,938 ⟶ 3,040:
 
see "Sexy prime triplets under 100:" + nl +nl
for n = 1 to len(primes)-2
for m = n + 1 to len(primes)-1
for x = m + 1 to len(primes)
bool1 = (primes[m] - primes[n] = 6)
Line 1,953 ⟶ 3,055:
 
see "Sexy prime quadruplets under 100:" + nl + nl
for n = 1 to len(primes)-3
for m = n + 1 to len(primes)-2
for x = m + 1 to len(primes)-1
for y = m + 1 to len(primes)
bool1 = (primes[m] - primes[n] = 6)
Line 1,971 ⟶ 3,073:
 
see "Sexy prime quintuplets under 100:" + nl + nl
for n = 1 to len(primes)-4
for m = n + 1 to len(primes)-3
for x = m + 1 to len(primes)-2
for y = m + 1 to len(primes)-1
for z = y + 1 to len(primes)
bool1 = (primes[m] - primes[n] = 6)
Line 1,990 ⟶ 3,092:
next
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,033 ⟶ 3,135:
 
(5 11 17 23 29)
 
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
<lang Ruby>
require 'prime'
 
Line 2,065 ⟶ 3,166:
unsexy.last(10).each {|item| print prime_array[item], " "}
print "\n\n", Time.now - start, " seconds"
</syntaxhighlight>
</lang>
 
Output:
Line 2,103 ⟶ 3,204:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.2"
// circular-queue = "0.2.5"
Line 2,170 ⟶ 3,271:
.collect::<Vec<String>>()
.join(", ")
}</langsyntaxhighlight>
 
{{out}}
Line 2,196 ⟶ 3,297:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">/* We could reduce the number of functions through a polymorphism since we're trying to retrieve sexy N-tuples (pairs, triplets etc...)
but one practical solution would be to use the Shapeless library for this purpose; here we only use built-in Scala packages. */
 
Line 2,283 ⟶ 3,384:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,299 ⟶ 3,400:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var limit = 1e6+35
var primes = limit.primes
 
Line 2,320 ⟶ 3,421:
var unsexy_primes = primes.grep {|p| is_prime(p+6) || is_prime(p-6) -> not }
say "...total number of unsexy primes = #{unsexy_primes.len.commify}"
say "...where last 10 unsexy primes are: #{unsexy_primes.last(10)}"</langsyntaxhighlight>
{{out}}
<pre>
Line 2,346 ⟶ 3,447:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
import "./math" for Int
var printHelper = Fn.new { |cat, le, lim, max|
Line 2,408 ⟶ 3,509:
unwrap.call(printHelper.call("unsexy primes", unsexy.count, lim, 10))
System.print("The last %(n) %(verb):\n %(unsexy[le-n..-1])\n")</langsyntaxhighlight>
 
{{out}}
Line 2,431 ⟶ 3,532:
The last 10 are:
[999853, 999863, 999883, 999907, 999917, 999931, 999961, 999979, 999983, 1000003]
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
if (N&1) = 0 then \even >2\ return false;
for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false;
I:= I+1;
];
return true;
];
 
int CU, C2, C3, C4, C5, N, I;
int Unsexy(10), Pairs(5), Trips(5), Quads(5), Quins(5);
[CU:= 0; C2:= 0; C3:= 0; C4:= 0; C5:= 0;
for N:= 1000035 downto 2 do
[if IsPrime(N) then
[if IsPrime(N-6) then
[if C2 < 5 then Pairs(C2):= N;
C2:= C2+1;
if IsPrime(N-12) then
[if C3 < 5 then Trips(C3):= N;
C3:= C3+1;
if IsPrime(N-18) then
[if C4 < 5 then Quads(C4):= N;
C4:= C4+1;
if IsPrime(N-24) then
[if C5 < 5 then Quins(C5):= N;
C5:= C5+1;
];
];
];
]
else if not IsPrime(N+6) then
[if CU < 10 then Unsexy(CU):= N;
CU:= CU+1;
];
];
];
IntOut(0, C2); Text(0, " pairs ending with:^m^j");
for I:= 4 downto 0 do
[Text(0, " [");
IntOut(0, Pairs(I)-6); Text(0, ", ");
IntOut(0, Pairs(I)); Text(0, "]^m^j");
];
IntOut(0, C3); Text(0, " triplets ending with:^m^j");
for I:= 4 downto 0 do
[Text(0, " [");
IntOut(0, Trips(I)-12); Text(0, ", ");
IntOut(0, Trips(I)-6); Text(0, ", ");
IntOut(0, Trips(I)); Text(0, "]^m^j");
];
IntOut(0, C4); Text(0, " quadruplets ending with:^m^j");
for I:= 4 downto 0 do
[Text(0, " [");
IntOut(0, Quads(I)-18); Text(0, ", ");
IntOut(0, Quads(I)-12); Text(0, ", ");
IntOut(0, Quads(I)-6); Text(0, ", ");
IntOut(0, Quads(I)); Text(0, "]^m^j");
];
IntOut(0, C5); Text(0, " quintuplet(s) ending with:^m^j");
I:= if C5 > 5 then 5 else C5;
for I:= I-1 downto 0 do
[Text(0, " [");
IntOut(0, Quins(I)-24); Text(0, ", ");
IntOut(0, Quins(I)-18); Text(0, ", ");
IntOut(0, Quins(I)-12); Text(0, ", ");
IntOut(0, Quins(I)-6); Text(0, ", ");
IntOut(0, Quins(I)); Text(0, "]^m^j");
];
IntOut(0, CU); Text(0, " unsexy primes ending with:^m^j");
for I:= 9 downto 0 do
[IntOut(0, Unsexy(I)); if I then Text(0, ", ")];
CrLf(0);
]</syntaxhighlight>
 
{{out}}
<pre>
16386 pairs ending with:
[999371, 999377]
[999431, 999437]
[999721, 999727]
[999763, 999769]
[999953, 999959]
2900 triplets ending with:
[997427, 997433, 997439]
[997541, 997547, 997553]
[998071, 998077, 998083]
[998617, 998623, 998629]
[998737, 998743, 998749]
325 quadruplets ending with:
[977351, 977357, 977363, 977369]
[983771, 983777, 983783, 983789]
[986131, 986137, 986143, 986149]
[990371, 990377, 990383, 990389]
[997091, 997097, 997103, 997109]
1 quintuplet(s) ending with:
[5, 11, 17, 23, 29]
48627 unsexy primes ending with:
999853, 999863, 999883, 999907, 999917, 999931, 999961, 999979, 999983, 1000003
</pre>
 
Line 2,438 ⟶ 3,641:
 
[[Extensible prime generator#zkl]] could be used instead.
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
const N=1_000_035, M=N+24; // M allows prime group to span N, eg N=100, (97,103)
const OVR=6; // 6 if prime group can NOT span N, else 0
Line 2,465 ⟶ 3,668:
println("Number of %s less than %,d is %,d".fmt(s,N,ps.len()));
println("The last %d %s:\n %s\n".fmt(n, (n>1 and "are" or "is"), gs));
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:80%">
2,123

edits