Anti-primes: Difference between revisions

 
(10 intermediate revisions by 5 users not shown)
Line 309:
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">BEGIN # find some anti-primes: numbers with more divisors than the #
BEGIN # find some anti-primes: numbers with more divisors than the #
# previous numbers #
REF[]INT maxndc number := 10HEAP[ 0001 : 0 ]INT; # table of divisor counts #
INT max divisors := 0;
INT a count := 0;
# construct a table of the divisor counts #
[ 1 : max number ]INT ndc; FOR in FROMWHILE 1a TOcount UPB< ndc20 DO ndc[ i ] := 1 OD;
FOR i FROM 2 TOIF n > UPB ndc DOTHEN
FOR j FROM i BY# ineed TOa UPBbigger ndctable DOof ndc[divisor jcounts ] +:= 1 OD #
ndc := HEAP[ 1 : UPB ndc + 5 000 ]INT;
OD;
FOR i FROM 1 TO UPB ndc DO ndc[ i ] := 1 OD;
# show the numbers with more divisors than their predecessors #
FOR i FROM 2 TO UPB ndc DO
INT a count := 0;
FOR j FROM i BY i TO UPB ndc WHILEDO andc[ countj <] 20+:= DO1 OD
INT divisor count = ndc[ i ];OD
FI;
IF divisor count > max divisors THEN
IF ndc[ n ] print(> (max "divisors ", whole( i, 0 ) ) );THEN
maxprint( divisors( :=" divisor", countwhole( n, 0 ) ) );
max divisors := ndc[ n ];
a count +:= 1
FI
OD;
print( ( newline ) )
END
END</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
Line 1,620 ⟶ 1,623:
=={{header|Delphi}}==
See [[#Pascal]].
=={{header|EasyLang}}==
{{trans|FutureBasic}}
<syntaxhighlight lang=easylang>
func divcnt v .
n = v
tot = 1
p = 2
while p <= sqrt n
cnt = 1
while n mod p = 0
cnt += 1
n = n div p
.
p += 1
tot *= cnt
.
if n > 1
tot *= 2
.
return tot
.
while count < 20
n += 1
divs = divcnt n
if divs > max
print n
max = divs
count += 1
.
.
</syntaxhighlight>
 
=={{header|Elixir}}==
{{trans|Erlang}}<syntaxhighlight lang="elixir">defmodule AntiPrimes do
Line 2,231 ⟶ 2,266:
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|Grain}}==
<syntaxhighlight lang="haskell">
import File from "sys/file"
let mut maxDiv = 0
let mut count = 0
let numaprimes = 20
let countDivisors = n => {
if (n < 1) {
1
} else {
let mut count = 2
let mut target = n / 2
for (let mut i = 1; i <= target; i += 1) {
if (n % i == 0) {
count += 1
} else {
void
}
}
count
}
}
print("\nThe first 20 anti-primes are: ")
let mut d = 0
for (let mut j = 1; count < numaprimes; j += 1) {
d = countDivisors(j)
if (d > maxDiv) {
File.fdWrite(File.stdout, Pervasives.toString(j))
File.fdWrite(File.stdout, " ")
maxDiv = d
count += 1
}
}
</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>
 
Line 2,628 ⟶ 2,703:
=={{header|langur}}==
{{trans|D}}
<syntaxhighlight lang="langur">val .countDivisors = ffn(.n) {
if .n < 2: return 1
for[=2] .i = 2; .i <= .n\2; .i += 1 {
Line 2,653 ⟶ 2,728:
 
=={{header|Lua}}==
 
===Counting the factors using modulo===
<syntaxhighlight lang="lua">-- First 20 antiprimes.
 
Line 2,713 ⟶ 2,790:
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
ndc = {}
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
oo.write( table.concat( antiprimes( 20 ), " " ) )
</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}}==
Line 2,751 ⟶ 2,867:
{{out}}
<pre>{1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560,10080,15120,20160,25200,27720}</pre>
 
=={{header|MiniScript}}==
{{Trans|Lua|Using a table of divisor counts}}
<syntaxhighlight lang="miniscript">
// Find the first 20 antiprimes.
 
// returns a table of the first goal antiprimes
antiprimes = function(goal)
maxNumber = 0
ndc = [] // table of divisor counts - initially empty
list = [0] * goal; number = 1; mostFactors = 0
aCount = 0
while aCount < goal
if number > maxNumber then
// need a bigger table of divisor counts
maxNumber = maxNumber + 5000
ndc = [1] * ( maxNumber + 1 )
ndc[ 0 ] = 0
for i in range( 2, maxNumber )
for j in range( i, maxNumber, i )
ndc[ j ] = ndc[ j ] + 1
end for
end for
end if
factors = ndc[ number ]
if factors > mostFactors then
list[ aCount ] = number
mostFactors = factors
aCount = aCount + 1
end if
number = number + 1
end while
return list
end function
 
// display the antiprimes
print antiprimes( 20 ).join( " " )
</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|Modula-2}}==
Line 3,688 ⟶ 3,846:
 
The &nbsp; #DIVS &nbsp; function could be further optimized by only processing &nbsp; ''even'' &nbsp; numbers, with unity being treated as a special case.
<syntaxhighlight lang="rexx">/*REXX program finds and displays N number of anti─primes oranti-primes highly─composite(highly-composite) numbers.*/
parseParse argArg N . /* obtain optional argument from the CL. */
ifIf N=='' | N=="," thenThen N= 20 /* Not specified? Then use the default. */
maxD= 0 /* the maximum number of divisors so far */
saySay '─index─-index- ──anti─prime──--anti-prime--' /* display a title forFor the numbers shown */
#nn= 0 /* the count of anti─primesanti-primes found " " */
Do i=1 For 59 While nn<N /* step through possible numbers by twos */
do once=1 for 1
d=nndivs(i) do i=1 for 59 /* get nn divisors; /*step through possible numbers by twos*/
If d>maxD Then Do d= #divs(i); if d<=maxD then iterate /*get #found divisors;an anti-prime Isnn set new maxD too small? Skip.*/
maxD=d
#= # + 1; maxD= d /*found an anti─prime #; set new minD.*/
nn=nn+1
say center(#, 7) right(i, 10) /*display the index and the anti─prime.*/
Say center(nn,7) if #>=N then leave once right(i,10) /*if wedisplay havethe enoughindex anti─primes,and donethe anti-prime. */
end /*i*/End
End /*i*/
 
Do do ji=60 by 20 While nn<N /* step through possible numbers by 20. */
d=nndivs(i)
d= #divs(j); if d<=maxD then iterate /*get # divisors; Is too small? Skip.*/
If d>maxD Then Do #= # + 1; maxD= d /* found an anti─prime #;anti-prime nn set new minD.maxD */
maxD=d
say center(#, 7) right(j, 10) /*display the index and the anti─prime.*/
nn=nn+1
if #>=N then leave /*if we have enough anti─primes, done. */
Say center(nn,7) right(i,10) /* display the index and the anti-prime. */
end /*j*/
End
end /*once*/
End /*i*/
exit /*stick a fork in it, we're all done. */
Exit /* stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*-----------------------------------------------------------------------------------*/
#divs: procedure; parse arg x 1 y /*X and Y: both set from 1st argument.*/
nndivs: Procedure if x<3 then return x /* compute the number of proper /*handle special cases for onedivisors and two.*/
Parse Arg x
if x==4 then return 3 /* " " " " four. */
If x<2 Then
if x<6 then return 2 /* " " " " three or five*/
Return 1
odd= x // 2 /*check if X is odd or not. */
odd=x//2
if odd then #= 1 /*Odd? Assume Pdivisors count of 1.*/
n=1 else do; #= 3; y= x % 2 /*Even? " /* 1 is a proper divisor " " " 3.*/
Do j=2+odd by 1+odd While j*j<x /* test all endpossible integers /* [↑] start with known num of Pdivs.*/
/* up To but excluding sqrt(x) */
 
If x//j==0 Then do k=3 for x%2-3 by 1+odd while k<y /*for oddj numbersis a divisor,so is x%j skip evens.*/
n=n+2
if x//k==0 then do; #= # + 2 /*if no remainder, then found a divisor*/
End
y= x % k /*bump # Pdivs, calculate limit Y. */
If j*j==x Then /* If x is a square if k>=y then do; #= # - 1; leave; end /*limit?*/
n=n+1 end /* sqrt(x) is a proper divisor ___ */
n=n+1 else if k /*k> x is a proper divisor then leave /*only divide up to x */
Return n</syntaxhighlight>
end /*k*/ /* [↑] this form of DO loop is faster.*/
return #+1 /*bump "proper divisors" to "divisors".*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 20 </tt>}}
<pre>
Line 3,858 ⟶ 4,016:
 
=={{header|Ring}}==
===Counting the divisors using modulo===
<syntaxhighlight lang="ring">
# Project : Anti-primes
Line 3,909 ⟶ 4,068:
 
done...
</pre>
 
===Counting the divisors using a table===
 
<syntaxhighlight lang="ring">
# find the first 20 antiprimes
# - numbers woth more divisors than the previous numbers
 
numberOfDivisorCounts = 0
maxDivisor = 0
num = 0
n = 0
result = list(20)
while num < 20
n += 1
if n > numberOfDivisorCounts
# need a bigger table of divisor counts
numberOfDivisorCounts += 5000
ndc = list(numberOfDivisorCounts)
for i = 1 to numberOfDivisorCounts
ndc[ i ] = 1
next
for i = 2 to numberOfDivisorCounts
j = i
while j <= numberOfDivisorCounts
ndc[ j ] = ndc[ j ] + 1
j += i
end
next
ok
div = ndc[ n ]
if (div > maxDivisor)
maxDivisor = div
num += 1
result[num] = n
ok
end
see result[1]
for n = 2 to len(result)
see " " + string(result[n])
next
</syntaxhighlight>
 
{{out}}
<pre>
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
Line 4,260 ⟶ 4,465:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
System.print("The first 20 anti-primes are:")
890

edits