Anti-primes: Difference between revisions
Content added Content deleted
(→{{header|GW-BASIC}}: Clarified implementation of BASIC.) |
(Dialects of BASIC moved to the BASIC section.) |
||
Line 620: | Line 620: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header| |
==={{header|BASIC256}}=== |
||
{{works with|BASICA}} |
|||
<syntaxhighlight lang="gwbasic">10 REM Anti-primes |
|||
20 DEFINT A-Z |
|||
30 N=1 |
|||
40 IF S>=20 THEN END ELSE F=1 |
|||
50 IF N<2 GOTO 80 ELSE FOR I=1 TO N\2 |
|||
60 IF N MOD I=0 THEN F=F+1 |
|||
70 NEXT |
|||
80 IF F<=M GOTO 120 |
|||
90 PRINT N, |
|||
100 M=F |
|||
110 S=S+1 |
|||
120 N=N+1 |
|||
130 GOTO 40</syntaxhighlight> |
|||
{{out}} |
|||
<pre> 1 2 4 6 12 |
|||
24 36 48 60 120 |
|||
180 240 360 720 840 |
|||
1260 1680 2520 5040 7560</pre> |
|||
=={{header|BASIC256}}== |
|||
<syntaxhighlight lang="basic256"> |
<syntaxhighlight lang="basic256"> |
||
Dim Results(20) |
Dim Results(20) |
||
Line 674: | Line 653: | ||
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
||
</pre> |
</pre> |
||
==={{header|FreeBASIC}}=== |
|||
<syntaxhighlight lang="freebasic"> |
|||
' convertido desde Ada |
|||
Declare Function count_divisors(n As Integer) As Integer |
|||
Dim As Integer max_divisors, divisors, results(1 To 20), candidate, j |
|||
candidate = 1 |
|||
Function count_divisors(n As Integer) As Integer |
|||
Dim As Integer i, count = 1 |
|||
For i = 1 To n/2 |
|||
If (n Mod i) = 0 Then count += 1 |
|||
Next i |
|||
count_divisors = count |
|||
End Function |
|||
Print "Los primeros 20 anti-primos son:" |
|||
For j = 1 To 20 |
|||
Do |
|||
divisors = count_divisors(Candidate) |
|||
If max_divisors < divisors Then |
|||
Results(j) = Candidate |
|||
max_divisors = divisors |
|||
Exit Do |
|||
End If |
|||
Candidate += 1 |
|||
Loop |
|||
Next j |
|||
For j = 1 To 20 |
|||
Print Results(j); |
|||
Next j |
|||
Print |
|||
Sleep |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Los primeros 20 anti-primos son: |
|||
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
|||
</pre> |
|||
==={{header|GW-BASIC}}=== |
|||
{{works with|BASICA}} |
|||
<syntaxhighlight lang="gwbasic">10 REM Anti-primes |
|||
20 DEFINT A-Z |
|||
30 N=1 |
|||
40 IF S>=20 THEN END ELSE F=1 |
|||
50 IF N<2 GOTO 80 ELSE FOR I=1 TO N\2 |
|||
60 IF N MOD I=0 THEN F=F+1 |
|||
70 NEXT |
|||
80 IF F<=M GOTO 120 |
|||
90 PRINT N, |
|||
100 M=F |
|||
110 S=S+1 |
|||
120 N=N+1 |
|||
130 GOTO 40</syntaxhighlight> |
|||
{{out}} |
|||
<pre> 1 2 4 6 12 |
|||
24 36 48 60 120 |
|||
180 240 360 720 840 |
|||
1260 1680 2520 5040 7560</pre> |
|||
Another solution: |
|||
{{works with|BASICA}} |
|||
<syntaxhighlight lang="gwbasic">10 REM Anti-primes |
|||
20 C = -999 |
|||
30 N = N + 1 |
|||
40 GOSUB 70 |
|||
50 IF T = 20 THEN END |
|||
60 GOTO 30 |
|||
70 D = 0 |
|||
80 FOR F = 1 TO INT(N/2) |
|||
90 IF N MOD F = 0 THEN D = D + 1 |
|||
100 NEXT F |
|||
110 IF D > C THEN GOSUB 130 |
|||
120 RETURN |
|||
130 C = D |
|||
140 T = T + 1 |
|||
150 PRINT N |
|||
160 RETURN</syntaxhighlight> |
|||
{{out}} |
|||
<pre> 1 |
|||
2 |
|||
4 |
|||
6 |
|||
12 |
|||
24 |
|||
36 |
|||
48 |
|||
60 |
|||
120 |
|||
180 |
|||
240 |
|||
360 |
|||
720 |
|||
840 |
|||
1260 |
|||
1680 |
|||
2520 |
|||
5040 |
|||
7560</pre> |
|||
==={{header|PureBasic}}=== |
|||
{{trans|C}}<syntaxhighlight lang="purebasic">Procedure.i cntDiv(n.i) |
|||
Define.i i, count |
|||
If n < 2 : ProcedureReturn 1 : EndIf |
|||
count = 2 : i = 2 |
|||
While i <= n / 2 |
|||
If n % i = 0 : count + 1 : EndIf |
|||
i + 1 |
|||
Wend |
|||
ProcedureReturn count |
|||
EndProcedure |
|||
; - - - MAIN - - - |
|||
Define.i n = 1, d, maxDiv = 0, count = 0 |
|||
If OpenConsole("") |
|||
PrintN("The first 20 anti-primes are: ") |
|||
While count < 20 |
|||
d = cntDiv(n) |
|||
If d > maxDiv |
|||
Print(Str(n) + " ") |
|||
maxDiv = d : count + 1 |
|||
EndIf |
|||
n + 1 |
|||
Wend |
|||
PrintN("") |
|||
Input() |
|||
EndIf |
|||
End 0</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The first 20 anti-primes are: |
|||
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
|||
</pre> |
|||
==={{header|Tiny BASIC}}=== |
|||
<syntaxhighlight lang="tiny basic">100 LET A=0 |
|||
101 LET N=1 |
|||
102 LET H=0 |
|||
103 PRINT "The first 20 anti-primes are:" |
|||
105 GOSUB 150 |
|||
106 LET H=F |
|||
107 LET A=A+1 |
|||
108 PRINT N |
|||
109 LET N=N+1 |
|||
110 IF A<20 THEN GOTO 105 |
|||
111 END |
|||
150 GOSUB 200 |
|||
151 IF F>H THEN RETURN |
|||
152 LET N=N+1 |
|||
153 GOTO 150 |
|||
200 LET F=0 |
|||
201 LET C=1 |
|||
205 IF N/C*C=N THEN LET F=F+1 |
|||
206 LET C=C+1 |
|||
207 IF C<=N THEN GOTO 205 |
|||
208 RETURN |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The first 20 anti-primes are: |
|||
1 |
|||
2 |
|||
4 |
|||
6 |
|||
12 |
|||
24 |
|||
36 |
|||
48 |
|||
60 |
|||
120 |
|||
180 |
|||
240 |
|||
360 |
|||
720 |
|||
840 |
|||
1260 |
|||
1680 |
|||
2520 |
|||
5040 |
|||
7560 |
|||
</pre> |
|||
==={{header|VBA}}=== |
|||
{{trans|Phix}} |
|||
<syntaxhighlight lang="vb">Private Function factors(n As Integer) As Collection |
|||
Dim f As New Collection |
|||
For i = 1 To Sqr(n) |
|||
If n Mod i = 0 Then |
|||
f.Add i |
|||
If n / i <> i Then f.Add n / i |
|||
End If |
|||
Next i |
|||
f.Add n |
|||
Set factors = f |
|||
End Function |
|||
Public Sub anti_primes() |
|||
Dim n As Integer, maxd As Integer |
|||
Dim res As New Collection, lenght As Integer |
|||
Dim lf As Integer |
|||
n = 1: maxd = -1 |
|||
Length = 0 |
|||
Do While res.count < 20 |
|||
lf = factors(n).count |
|||
If lf > maxd Then |
|||
res.Add n |
|||
maxd = lf |
|||
End If |
|||
n = n + IIf(n > 1, 2, 1) |
|||
Loop |
|||
Debug.Print "The first 20 anti-primes are:"; |
|||
For Each x In res |
|||
Debug.Print x; |
|||
Next x |
|||
End Sub</syntaxhighlight>{{out}} |
|||
<pre>The first 20 anti-primes are: 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
|||
</pre> |
|||
==={{header|Visual Basic .NET}}=== |
|||
{{trans|D}} |
|||
<syntaxhighlight lang="vbnet">Module Module1 |
|||
Function CountDivisors(n As Integer) As Integer |
|||
If n < 2 Then |
|||
Return 1 |
|||
End If |
|||
Dim count = 2 '1 and n |
|||
For i = 2 To n \ 2 |
|||
If n Mod i = 0 Then |
|||
count += 1 |
|||
End If |
|||
Next |
|||
Return count |
|||
End Function |
|||
Sub Main() |
|||
Dim maxDiv, count As Integer |
|||
Console.WriteLine("The first 20 anti-primes are:") |
|||
Dim n = 1 |
|||
While count < 20 |
|||
Dim d = CountDivisors(n) |
|||
If d > maxDiv Then |
|||
Console.Write("{0} ", n) |
|||
maxDiv = d |
|||
count += 1 |
|||
End If |
|||
n += 1 |
|||
End While |
|||
Console.WriteLine() |
|||
End Sub |
|||
End Module</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The first 20 anti-primes are: |
|||
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560</pre> |
|||
==={{header|Yabasic}}=== |
|||
{{Output?|Yabasic}} |
|||
{{trans|AWK}} |
|||
<syntaxhighlight lang="yabasic">print "The first 20 anti-primes are:" |
|||
while (count < 20) |
|||
n = n + 1 |
|||
d = count_divisors(n) |
|||
if d > max_divisors then |
|||
print n; |
|||
max_divisors = d |
|||
count = count + 1 |
|||
end if |
|||
wend |
|||
print |
|||
sub count_divisors(n) |
|||
local count, i |
|||
if n < 2 return 1 |
|||
count = 2 |
|||
for i = 2 to n/2 |
|||
if not(mod(n, i)) count = count + 1 |
|||
next |
|||
return count |
|||
end sub</syntaxhighlight> |
|||
{{trans|Lua}} |
|||
<syntaxhighlight lang="yabasic">// First 20 antiprimes. |
|||
sub count_factors(number) |
|||
local count, attempt |
|||
for attempt = 1 to number |
|||
if not mod(number, attempt) count = count + 1 |
|||
next |
|||
return count |
|||
end sub |
|||
sub antiprimes$(goal) |
|||
local factors, list$, number, mostFactors, nitems |
|||
number = 1 |
|||
while nitems < goal |
|||
factors = count_factors(number) |
|||
if factors > mostFactors then |
|||
list$ = list$ + ", " + str$(number) |
|||
nitems = nitems + 1 |
|||
mostFactors = factors |
|||
endif |
|||
number = number + 1 |
|||
wend |
|||
return list$ |
|||
end sub |
|||
print "The first 20 antiprimes:" |
|||
print mid$(antiprimes$(20), 3) |
|||
print "Done."</syntaxhighlight> |
|||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
Line 1,534: | Line 1,831: | ||
The first 20 anti-primes are: |
The first 20 anti-primes are: |
||
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
||
</pre> |
|||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic"> |
|||
' convertido desde Ada |
|||
Declare Function count_divisors(n As Integer) As Integer |
|||
Dim As Integer max_divisors, divisors, results(1 To 20), candidate, j |
|||
candidate = 1 |
|||
Function count_divisors(n As Integer) As Integer |
|||
Dim As Integer i, count = 1 |
|||
For i = 1 To n/2 |
|||
If (n Mod i) = 0 Then count += 1 |
|||
Next i |
|||
count_divisors = count |
|||
End Function |
|||
Print "Los primeros 20 anti-primos son:" |
|||
For j = 1 To 20 |
|||
Do |
|||
divisors = count_divisors(Candidate) |
|||
If max_divisors < divisors Then |
|||
Results(j) = Candidate |
|||
max_divisors = divisors |
|||
Exit Do |
|||
End If |
|||
Candidate += 1 |
|||
Loop |
|||
Next j |
|||
For j = 1 To 20 |
|||
Print Results(j); |
|||
Next j |
|||
Print |
|||
Sleep |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Los primeros 20 anti-primos son: |
|||
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
|||
</pre> |
</pre> |
||
Line 1,596: | Line 1,853: | ||
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
||
</pre> |
</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 1,661: | Line 1,917: | ||
Output: |
Output: |
||
<pre>[1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560]</pre> |
<pre>[1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560]</pre> |
||
=={{header|GW-BASIC}}== |
|||
<syntaxhighlight lang="gwbasic">10 C = -999 |
|||
20 N = N + 1 |
|||
30 GOSUB 60 |
|||
40 IF T = 20 THEN END |
|||
50 GOTO 20 |
|||
60 D = 0 |
|||
70 FOR F = 1 TO INT(N/2) |
|||
80 IF N MOD F = 0 THEN D = D + 1 |
|||
90 NEXT F |
|||
100 IF D > C THEN GOSUB 120 |
|||
110 RETURN |
|||
120 C = D |
|||
130 T = T + 1 |
|||
140 PRINT N |
|||
150 RETURN</syntaxhighlight> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 2,617: | Line 2,855: | ||
<pre> |
<pre> |
||
The first twenty anti-primes are [1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560] |
The first twenty anti-primes are [1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560] |
||
</pre> |
|||
=={{header|PureBasic}}== |
|||
{{trans|C}}<syntaxhighlight lang="purebasic">Procedure.i cntDiv(n.i) |
|||
Define.i i, count |
|||
If n < 2 : ProcedureReturn 1 : EndIf |
|||
count = 2 : i = 2 |
|||
While i <= n / 2 |
|||
If n % i = 0 : count + 1 : EndIf |
|||
i + 1 |
|||
Wend |
|||
ProcedureReturn count |
|||
EndProcedure |
|||
; - - - MAIN - - - |
|||
Define.i n = 1, d, maxDiv = 0, count = 0 |
|||
If OpenConsole("") |
|||
PrintN("The first 20 anti-primes are: ") |
|||
While count < 20 |
|||
d = cntDiv(n) |
|||
If d > maxDiv |
|||
Print(Str(n) + " ") |
|||
maxDiv = d : count + 1 |
|||
EndIf |
|||
n + 1 |
|||
Wend |
|||
PrintN("") |
|||
Input() |
|||
EndIf |
|||
End 0</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The first 20 anti-primes are: |
|||
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
|||
</pre> |
</pre> |
||
Line 3,260: | Line 3,465: | ||
./anti_primes.tcl |
./anti_primes.tcl |
||
The first 20 anti-primes are: |
The first 20 anti-primes are: |
||
1 |
|||
2 |
|||
4 |
|||
6 |
|||
12 |
|||
24 |
|||
36 |
|||
48 |
|||
60 |
|||
120 |
|||
180 |
|||
240 |
|||
360 |
|||
720 |
|||
840 |
|||
1260 |
|||
1680 |
|||
2520 |
|||
5040 |
|||
7560 |
|||
</pre> |
|||
=={{header|Tiny BASIC}}== |
|||
<syntaxhighlight lang="tiny basic">100 LET A=0 |
|||
101 LET N=1 |
|||
102 LET H=0 |
|||
103 PRINT "The first 20 anti-primes are:" |
|||
105 GOSUB 150 |
|||
106 LET H=F |
|||
107 LET A=A+1 |
|||
108 PRINT N |
|||
109 LET N=N+1 |
|||
110 IF A<20 THEN GOTO 105 |
|||
111 END |
|||
150 GOSUB 200 |
|||
151 IF F>H THEN RETURN |
|||
152 LET N=N+1 |
|||
153 GOTO 150 |
|||
200 LET F=0 |
|||
201 LET C=1 |
|||
205 IF N/C*C=N THEN LET F=F+1 |
|||
206 LET C=C+1 |
|||
207 IF C<=N THEN GOTO 205 |
|||
208 RETURN |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The first 20 anti-primes are: |
|||
1 |
1 |
||
2 |
2 |
||
Line 3,382: | Line 3,540: | ||
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
||
</pre> |
</pre> |
||
=={{header|VBA}}== |
|||
{{trans|Phix}} |
|||
<syntaxhighlight lang="vb">Private Function factors(n As Integer) As Collection |
|||
Dim f As New Collection |
|||
For i = 1 To Sqr(n) |
|||
If n Mod i = 0 Then |
|||
f.Add i |
|||
If n / i <> i Then f.Add n / i |
|||
End If |
|||
Next i |
|||
f.Add n |
|||
Set factors = f |
|||
End Function |
|||
Public Sub anti_primes() |
|||
Dim n As Integer, maxd As Integer |
|||
Dim res As New Collection, lenght As Integer |
|||
Dim lf As Integer |
|||
n = 1: maxd = -1 |
|||
Length = 0 |
|||
Do While res.count < 20 |
|||
lf = factors(n).count |
|||
If lf > maxd Then |
|||
res.Add n |
|||
maxd = lf |
|||
End If |
|||
n = n + IIf(n > 1, 2, 1) |
|||
Loop |
|||
Debug.Print "The first 20 anti-primes are:"; |
|||
For Each x In res |
|||
Debug.Print x; |
|||
Next x |
|||
End Sub</syntaxhighlight>{{out}} |
|||
<pre>The first 20 anti-primes are: 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
|||
</pre> |
|||
=={{header|Visual Basic .NET}}== |
|||
{{trans|D}} |
|||
<syntaxhighlight lang="vbnet">Module Module1 |
|||
Function CountDivisors(n As Integer) As Integer |
|||
If n < 2 Then |
|||
Return 1 |
|||
End If |
|||
Dim count = 2 '1 and n |
|||
For i = 2 To n \ 2 |
|||
If n Mod i = 0 Then |
|||
count += 1 |
|||
End If |
|||
Next |
|||
Return count |
|||
End Function |
|||
Sub Main() |
|||
Dim maxDiv, count As Integer |
|||
Console.WriteLine("The first 20 anti-primes are:") |
|||
Dim n = 1 |
|||
While count < 20 |
|||
Dim d = CountDivisors(n) |
|||
If d > maxDiv Then |
|||
Console.Write("{0} ", n) |
|||
maxDiv = d |
|||
count += 1 |
|||
End If |
|||
n += 1 |
|||
End While |
|||
Console.WriteLine() |
|||
End Sub |
|||
End Module</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The first 20 anti-primes are: |
|||
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560</pre> |
|||
=={{header|V (Vlang)}}== |
=={{header|V (Vlang)}}== |
||
Line 3,544: | Line 3,626: | ||
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
||
</pre> |
</pre> |
||
=={{header|Yabasic}}== |
|||
{{Output?|Yabasic}} |
|||
{{trans|AWK}} |
|||
<syntaxhighlight lang="yabasic">print "The first 20 anti-primes are:" |
|||
while (count < 20) |
|||
n = n + 1 |
|||
d = count_divisors(n) |
|||
if d > max_divisors then |
|||
print n; |
|||
max_divisors = d |
|||
count = count + 1 |
|||
end if |
|||
wend |
|||
print |
|||
sub count_divisors(n) |
|||
local count, i |
|||
if n < 2 return 1 |
|||
count = 2 |
|||
for i = 2 to n/2 |
|||
if not(mod(n, i)) count = count + 1 |
|||
next |
|||
return count |
|||
end sub</syntaxhighlight> |
|||
{{trans|Lua}} |
|||
<syntaxhighlight lang="yabasic">// First 20 antiprimes. |
|||
sub count_factors(number) |
|||
local count, attempt |
|||
for attempt = 1 to number |
|||
if not mod(number, attempt) count = count + 1 |
|||
next |
|||
return count |
|||
end sub |
|||
sub antiprimes$(goal) |
|||
local factors, list$, number, mostFactors, nitems |
|||
number = 1 |
|||
while nitems < goal |
|||
factors = count_factors(number) |
|||
if factors > mostFactors then |
|||
list$ = list$ + ", " + str$(number) |
|||
nitems = nitems + 1 |
|||
mostFactors = factors |
|||
endif |
|||
number = number + 1 |
|||
wend |
|||
return list$ |
|||
end sub |
|||
print "The first 20 antiprimes:" |
|||
print mid$(antiprimes$(20), 3) |
|||
print "Done."</syntaxhighlight> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |