Anti-primes: Difference between revisions
Added Uiua solution
(Initial FutureBasic task solution added) |
(Added Uiua solution) |
||
(24 intermediate revisions by 8 users not shown) | |||
Line 309:
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
BEGIN # find some anti-primes: numbers with more divisors than the #
# previous numbers #
REF[]INT
INT max divisors := 0;
INT a count := 0;
ndc := HEAP[ 1 : UPB ndc + 5 000 ]INT;
FOR i FROM 1 TO UPB ndc DO ndc[ i ] := 1 OD;
FOR i FROM 2 TO UPB ndc DO
FOR j FROM i BY i TO UPB ndc
FI;
IF ndc[ n ]
max divisors := ndc[ n ];
a count +:= 1
FI
OD;
print( ( newline ) )
END
</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
if
for[=2]
if
}
}
writeln "The first 20 anti-primes are:"
var
for
val
if
write
}
}
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 2,993 ⟶ 3,151:
<pre>
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
=={{header|Odin}}==
Anti-Primes
Odin Build: dev-2023-07-nightly:3072479c
<syntaxhighlight lang="go">
package antiprimes
import "core:fmt"
main :: proc() {
AntiPrimeCount, MaxDivisors, Divisors, n: u64
MaxAntiPrime: u64 = 20
fmt.print("\nFirst 20 anti-primes\n")
fmt.println("--------------------")
for (AntiPrimeCount < MaxAntiPrime) {
n += 1
Divisors = DivisorCount(n)
if Divisors > MaxDivisors {
fmt.print(n, " ")
MaxDivisors = Divisors
AntiPrimeCount += 1
}
}
}
DivisorCount :: proc(v: u64) -> u64 {
total: u64 = 1
a := v
if a == 0 {
return 0
}
for a % 2 == 0 {
total += 1
a /= 2
}
for p: u64 = 3; p * p <= a; p += 2 {
count: u64 = 1
for a % p == 0 {
count += 1
a /= p
}
total *= count
}
if a > 1 {
total *= 2
}
return total
}
</syntaxhighlight>
{{out}}
<pre>
First 20 anti-primes
--------------------
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
Line 3,634 ⟶ 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,804 ⟶ 4,016:
=={{header|Ring}}==
===Counting the divisors using modulo===
<syntaxhighlight lang="ring">
# Project : Anti-primes
Line 3,855 ⟶ 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 3,861 ⟶ 4,120:
{{works with|Halcyon Calc|4.2.7}}
≪ → nb
≪ 1
1 nb 2 / '''FOR''' j
nb j MOD NOT +
'''NEXT'''
≫ ≫ ‘<span style="color:blue">NDIV</span>’ STO
≪ 1 - → items
≪ { } (2,0)
'''DO'''
DUP RE <span style="color:blue">NDIV</span>
'''IF''' OVER IM OVER < '''THEN'''
SWAP RE SWAP R→C
SWAP OVER RE + SWAP
'''ELSE''' DROP '''END'''
1
'''UNTIL''' OVER SIZE items > '''END'''
DROP
≫ ≫ ‘<span style="color:blue">ANTIP</span>’ STO
15 <span style="color:blue">ANTIP</span>
{{out}}
<pre>
Line 3,892 ⟶ 4,148:
<syntaxhighlight lang="ruby">require 'prime'
def num_divisors(n)
n.prime_division.inject(1){|prod, (_p,n)| prod *
end
Line 4,140 ⟶ 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,209 ⟶ 4,481:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="
System.print("The first 20 anti-primes are:")
|