Sexy primes: Difference between revisions
Content added Content deleted
(Added BASIC256 and Yabasic. Moved FreeBASIC to section BASIC) |
|||
Line 343: | Line 343: | ||
5 11 17 23 29, |
5 11 17 23 29, |
||
</pre> |
</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}}== |
=={{header|C}}== |
||
Line 866: | Line 1,106: | ||
Last 10: 999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003 |
Last 10: 999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003 |
||
</pre> |
</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}}== |
=={{header|Go}}== |