Anti-primes: Difference between revisions
Added Uiua solution
(Lua →Using a table of divisor counts: Simplify the display code) |
(Added Uiua solution) |
||
(8 intermediate revisions by 6 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
if
for[=2]
if
}
}
writeln "The first 20 anti-primes are:"
var
for
val
if
write
}
}
Line 2,811 ⟶ 2,883:
// need a bigger table of divisor counts
maxNumber = maxNumber + 5000
ndc = [
for i in range( 2, maxNumber )
for j in range( i, maxNumber, i )
Line 3,776 ⟶ 3,846:
The #DIVS function could be further optimized by only processing ''even'' numbers, with unity being treated as a special case.
<syntaxhighlight lang="rexx">/*REXX program finds and displays
maxD=
Do i=1 For 59 While nn<N /* step through possible numbers by twos */
d=nndivs(i)
If d>maxD Then Do
maxD=d
nn=nn+1
Say center(nn,7)
End /*i*/
Do
d=nndivs(i)
If d>maxD Then Do
maxD=d
nn=nn+1
Say center(nn,7) right(i,10) /* display the index and the anti-prime. */
End
End /*i*/
Exit /* stick a fork in it, we're all done. */
/*-----------------------------------------------------------------------------------*/
nndivs: Procedure
Parse Arg x
If x<2 Then
Return 1
odd=x//2
n=1
Do j=2+odd by 1+odd While j*j<x /* test all
/* up To but excluding sqrt(x) */
If x//j==0 Then
n=n+2
End
If j*j==x Then /* If x is a square
n=n+1
n=n+1
Return n</syntaxhighlight>
{{out|output|text= when using the default input of: <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,279 ⟶ 4,396:
<pre>
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
=={{header|Uiua}}==
<syntaxhighlight lang="uiua">
# slightly faster than the simplistic /+=0◿+1⇡.
NDivs ← ⟨+2/+=0◿(↘1+1⇡⌊÷2).|1⟩<2.
⇌⊙◌◌⍢(
# Inc n, get NDiv: if >m, join n to accum, store new m.
⟨◌|⟜(⊂⊢)⍜(⊡1|◌):⟩:⟜<NDivs°⊟.⍜⊢(+1)
|
⋅(<:⧻) # Repeat until we have enough values.
)0_0 [] 20 # [n m] [accum] limit
</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,481:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="
System.print("The first 20 anti-primes are:")
|