Anti-primes: Difference between revisions

(Lua →‎Using a table of divisor counts: Simplify the display code)
 
(6 intermediate revisions by 5 users not shown)
Line 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,234 ⟶ 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,631 ⟶ 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,811 ⟶ 2,883:
// need a bigger table of divisor counts
maxNumber = maxNumber + 5000
ndc = [01] * ( maxNumber + 1 )
forndc[ i0 in] range( 1,= maxNumber )0
ndc[ i ] = 1
end for
for i in range( 2, maxNumber )
for j in range( i, maxNumber, i )
Line 3,776 ⟶ 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,946 ⟶ 4,016:
 
=={{header|Ring}}==
===Counting the divisors using modulo===
<syntaxhighlight lang="ring">
# Project : Anti-primes
Line 3,997 ⟶ 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,348 ⟶ 4,465:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
System.print("The first 20 anti-primes are:")
890

edits