Anti-primes: Difference between revisions
→{{header|langur}}
Space Ghost (talk | contribs) |
Langurmonkey (talk | contribs) |
||
(14 intermediate revisions by 7 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 .countDivisors =
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,001 ⟶ 3,159:
package antiprimes
import "core:fmt"
main :: proc() {
Line 3,009 ⟶ 3,166:
fmt.println("--------------------")
for (AntiPrimeCount < MaxAntiPrime) {
Divisors = DivisorCount(n)
if Divisors > MaxDivisors {
Line 3,689 ⟶ 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,859 ⟶ 4,016:
=={{header|Ring}}==
===Counting the divisors using modulo===
<syntaxhighlight lang="ring">
# Project : Anti-primes
Line 3,910 ⟶ 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,916 ⟶ 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,947 ⟶ 4,148:
<syntaxhighlight lang="ruby">require 'prime'
def num_divisors(n)
n.prime_division.inject(1){|prod, (_p,n)| prod *
end
Line 4,264 ⟶ 4,465:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="
System.print("The first 20 anti-primes are:")
|