Sexy primes: Difference between revisions

7,292 bytes added ,  22 days ago
Added BASIC256 and Yabasic. Moved FreeBASIC to section BASIC
(→‎{{header|J}}: brute force approach)
(Added BASIC256 and Yabasic. Moved FreeBASIC to section BASIC)
 
(6 intermediate revisions by 4 users not shown)
Line 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}}==
Line 689 ⟶ 929:
=={{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#}}==
Line 786 ⟶ 1,106:
Last 10: 999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003
</pre>
 
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">Function isPrime(Byval ValorEval As Uinteger) As Boolean
If ValorEval < 2 Then Return False
If ValorEval Mod 2 = 0 Then Return ValorEval = 2
If ValorEval Mod 3 = 0 Then Return ValorEval = 3
Dim d As Integer = 5
While d * d <= ValorEval
If ValorEval Mod d = 0 Then Return False Else d += 2
If ValorEval Mod d = 0 Then Return False Else d += 4
Wend
Return True
End Function
 
#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|Go}}==
Line 1,165 ⟶ 1,381:
11
5
#unp=: p1-.,sp2-/ 0 6+/(#~ 1 p: NB.6+]) unsexyp1
48627
48628
_10{.unp
999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003 1000033
</syntaxhighlight>
 
Line 1,323 ⟶ 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>
 
Line 3,164 ⟶ 3,447:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Int
var printHelper = Fn.new { |cat, le, lim, max|
2,123

edits