Anti-primes: Difference between revisions

1,166 bytes added ,  10 months ago
→‎{{header|Lua}}: Added an alternative
(→‎{{header|Lua}}: Added an alternative)
Line 2,653:
 
=={{header|Lua}}==
 
===Counting the factors using modulo===
<syntaxhighlight lang="lua">-- First 20 antiprimes.
 
Line 2,713 ⟶ 2,715:
7560
Done.</pre>
 
===Using a table of divisor counts===
 
<syntaxhighlight lang="lua">
-- Find the first 20 antiprimes.
 
-- returns a table of the first goal antiprimes
function antiprimes(goal)
local maxNumber = 0
local ndc = {} -- table of divisor counts - initially empty
local list, number, mostFactors = {}, 1, 0
while #list < goal do
if number > #ndc then
-- need a bigger table of divisor counts
maxNumber = maxNumber + 5000
for i = 1, maxNumber do ndc[ i ] = 1 end
for i = 2, maxNumber do
for j = i, maxNumber, i do ndc[ j ] = ndc[ j ] + 1 end
end
end
local factors = ndc[ number ]
if factors > mostFactors then
table.insert( list, number )
mostFactors = factors
end
number = number + 1
end
return list
end
 
-- display the antiprimes
for _, item in ipairs( antiprimes( 20 ) ) do
io.write( string.format( " %d", item ) )
end
</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|Maple}}==
3,044

edits