Sequence: smallest number with exactly n divisors: Difference between revisions

m
No edit summary
m (→‎{{header|Wren}}: Minor tidy)
 
(24 intermediate revisions by 14 users not shown)
Line 20:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F divisors(n)
V divs = [1]
L(ii) 2 .< Int(n ^ 0.5) + 3
Line 45:
 
L(item) sequence(15)
print(item)</langsyntaxhighlight>
 
{{out}}
Line 64:
192
144
</pre>
 
=={{header|Action!}}==
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
<syntaxhighlight lang="action!">CARD FUNC CountDivisors(CARD a)
CARD i,count
 
i=1 count=0
WHILE i*i<=a
DO
IF a MOD i=0 THEN
IF i=a/i THEN
count==+1
ELSE
count==+2
FI
FI
i==+1
OD
RETURN (count)
 
PROC Main()
DEFINE MAX="15"
CARD a,count
BYTE i
CARD ARRAY seq(MAX)
 
FOR i=0 TO MAX-1
DO
seq(i)=0
OD
 
i=0 a=1
WHILE i<MAX
DO
count=CountDivisors(a)
IF count<=MAX AND seq(count-1)=0 THEN
seq(count-1)=a
i==+1
FI
a==+1
OD
 
FOR i=0 TO MAX-1
DO
IF i>0 THEN
Print(", ")
FI
PrintC(seq(i))
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sequence_smallest_number_with_exactly_n_divisors.png Screenshot from Atari 8-bit computer]
<pre>
1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|C}}
<langsyntaxhighlight lang="algol68">BEGIN
 
PROC count divisors = ( INT n )INT:
Line 102 ⟶ 157:
print( ( newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 108 ⟶ 163:
1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144
</pre>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">((0+.=⍳|⊢)¨∘(⍳2÷⍨2∘*)⍳⍳)15</syntaxhighlight>
{{out}}
<pre>1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">firstNumWithDivisors: function [n][
i: 0
while ø [
if n = size factors i -> return i
i: i+1
]
]
 
print map 1..15 => firstNumWithDivisors</syntaxhighlight>
 
{{out}}
 
<pre>1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre>
 
=={{header|AutoHotkey}}==
{{trans|Go}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">max := 15
seq := [], n := 0
while (n < max)
Line 126 ⟶ 202:
count += A_Index = n/A_Index ? 1 : 2
return count
}</langsyntaxhighlight>
Outputs:<pre>The first 15 terms of the sequence are:
1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SEQUENCE_SMALLEST_NUMBER_WITH_EXACTLY_N_DIVISORS.AWK
# converted from Kotlin
Line 161 ⟶ 237:
return(count)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
first 15 terms: 1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|Ring}}
<syntaxhighlight lang="vbnet">print "the first 15 terms of the sequence are:"
for n = 1 to 15
for m = 1 to 4100
pnum = 0
for p = 1 to 4100
if m % p = 0 then pnum += 1
next p
if pnum = n then
print m; " ";
exit for
end if
next m
next n</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{trans|Ring}}
{{works with|Chipmunk Basic|3.6.4}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="vbnet">100 print "the first 15 terms of the sequence are:"
110 for n = 1 to 15
120 for m = 1 to 4100
130 pnum = 0
140 for p = 1 to 4100
150 if m mod p = 0 then pnum = pnum+1
160 next p
170 if pnum = n then print m;" "; : goto 190
180 next m
190 next n
200 print "done..."
210 end</syntaxhighlight>
 
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|QBasic}}===
{{trans|Ring}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">PRINT "the first 15 terms of the sequence are:"
FOR n = 1 to 15
FOR m = 1 to 4100
pnum = 0
FOR p = 1 to 4100
IF m MOD p = 0 then pnum = pnum + 1
NEXT p
IF pnum = n then
PRINT m;
EXIT FOR
END IF
NEXT m
NEXT n</syntaxhighlight>
 
==={{header|Run BASIC}}===
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|True BASIC}}===
{{trans|Ring}}
<syntaxhighlight lang="qbasic">PRINT "the first 15 terms of the sequence are:"
FOR n = 1 to 15
FOR m = 1 to 4100
LET pnum = 0
FOR p = 1 to 4100
IF remainder(m, p) = 0 then LET pnum = pnum+1
NEXT p
IF pnum = n then
PRINT m;
EXIT FOR
END IF
NEXT m
NEXT n
PRINT "done..."
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{trans|Ring}}
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "program name"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
PRINT "the first 15 terms of the sequence are:"
FOR n = 1 TO 15
FOR m = 1 TO 4100
pnum = 0
FOR p = 1 TO 4100
IF m MOD p = 0 THEN INC pnum
NEXT p
IF pnum = n THEN
PRINT m;
EXIT FOR
END IF
NEXT m
NEXT n
END FUNCTION
END PROGRAM</syntaxhighlight>
 
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">Procedure.i divisors(n)
;find the number of divisors of an integer
Define.i r, i
r = 2
For i = 2 To n/2
If n % i = 0: r + 1
EndIf
Next i
ProcedureReturn r
EndProcedure
 
OpenConsole()
Define.i UPTO, i, n, nfound
 
UPTO = 15
i = 2
nfound = 1
Dim sfound.i(UPTO)
sfound(1) = 1
 
While nfound < UPTO
n = divisors(i)
If n <= UPTO And sfound(n) = 0:
nfound + 1
sfound(n) = i
EndIf
i + 1
Wend
 
Print("1 ") ;special case
For i = 2 To UPTO
Print(Str(sfound(i)) + " ")
Next i
 
PrintN(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">UPTO = 15
i = 2
nfound = 1
dim sfound(UPTO)
sfound(1) = 1
 
while nfound < UPTO
n = divisors(i)
if n <= UPTO and sfound(n) = 0 then
nfound = nfound + 1
sfound(n) = i
fi
i = i + 1
end while
 
print 1, " "; //special case
for i = 2 to UPTO
print sfound(i), " ";
next i
print
end
 
sub divisors(n)
local r, i
//find the number of divisors of an integer
r = 2
for i = 2 to n / 2
if mod(n, i) = 0 r = r + 1
next i
return r
end sub</syntaxhighlight>
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
 
manifest $( LENGTH = 15 $)
 
let divisors(n) = valof
$( let count = 0 and i = 1
while i*i <= n
$( if n rem i = 0 then
count := count + (i = n/i -> 1, 2)
i := i + 1
$)
resultis count
$)
 
let sequence(n, seq) be
$( let found = 0 and i = 1
for i=1 to n do seq!i := 0
while found < n
$( let divs = divisors(i)
if divs <= n & seq!divs = 0
$( found := found + 1
seq!divs := i
$)
i := i + 1
$)
$)
 
let start() be
$( let seq = vec LENGTH
sequence(LENGTH, seq)
for i=1 to LENGTH do writef("%N ", seq!i)
wrch('*N')
$)</syntaxhighlight>
{{out}}
<pre>1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre>
 
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdio.h>
 
#define MAX 15
Line 200 ⟶ 499:
printf("\n");
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 210 ⟶ 509:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">#include <iostream>
 
#define MAX 15
Line 243 ⟶ 542:
cout << endl;
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 250 ⟶ 549:
1 2 4 6 16 12 64 24 36 48 1024 60 0 192 144
</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
const AMOUNT := 15;
typedef I is uint16;
 
sub divisors(n: I): (count: I) is
var i: I := 1;
count := 0;
while i*i <= n loop
if n%i == 0 then
if n/i == i then
count := count + 1;
else
count := count + 2;
end if;
end if;
i := i + 1;
end loop;
end sub;
 
var seq: I[AMOUNT+1];
MemZero(&seq as [uint8], @bytesof seq);
 
var found: I := 0;
var i: I := 1;
 
while found < AMOUNT loop
var divs := divisors(i) as @indexof seq;
if divs <= AMOUNT and seq[divs] == 0 then
found := found + 1;
seq[divs] := i;
end if;
i := i + 1;
end loop;
 
var j: @indexof seq := 1;
while j <= AMOUNT loop
print_i16(seq[j]);
print_char(' ');
j := j + 1;
end loop;
print_nl();</syntaxhighlight>
{{out}}
<pre>1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
{This code would normally be in a separate library. It is provided for clarity}
 
function GetAllProperDivisors(N: Integer;var IA: TIntegerDynArray): integer;
{Make a list of all the "proper dividers" for N}
{Proper dividers are the of numbers the divide evenly into N}
var I: integer;
begin
SetLength(IA,0);
for I:=1 to N-1 do
if (N mod I)=0 then
begin
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=I;
end;
Result:=Length(IA);
end;
 
 
function GetAllDivisors(N: Integer;var IA: TIntegerDynArray): integer;
{Make a list of all the "proper dividers" for N, Plus N itself}
begin
Result:=GetAllProperDivisors(N,IA)+1;
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=N;
end;
 
 
{-------------------------------------------------------------------------------}
 
procedure SequenceWithNdivisors(Memo: TMemo);
var N,N2: integer;
var IA: TIntegerDynArray;
begin
for N:=1 to 15 do
begin
N2:=0;
repeat Inc(N2) until N=GetAllDivisors(N2,IA);
Memo.Lines.Add(IntToStr(N)+' - '+IntToStr(N2));
end;
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
1 - 1
2 - 2
3 - 4
4 - 6
5 - 16
6 - 12
7 - 64
8 - 24
9 - 36
10 - 48
11 - 1024
12 - 60
13 - 4096
14 - 192
15 - 144
 
Elapsed Time: 54.950 ms.
 
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func cntdiv n .
i = 1
while i <= sqrt n
if n mod i = 0
cnt += 1
if i <> sqrt n
cnt += 1
.
.
i += 1
.
return cnt
.
len seq[] 15
i = 1
while n < 15
k = cntdiv i
if k <= 15 and seq[k] = 0
seq[k] = i
n += 1
.
i += 1
.
for v in seq[]
print v
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Find Antı-Primes plus. Nigel Galloway: April 9th., 2019
// Increasing the value 14 will increase the number of anti-primes plus found
Line 263 ⟶ 713:
let n=Seq.concat(Seq.scan(fun n g->fE n (fG g)) (seq[(2147483647,1,1I)]) fI)|>List.ofSeq|>List.groupBy(fun(_,n,_)->n)|>List.sortBy(fun(n,_)->n)|>List.takeWhile(fun(n,_)->fL n)
for n,g in n do printfn "%d->%A" n (g|>List.map(fun(_,_,n)->n)|>List.min)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 328 ⟶ 778:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: fry kernel lists lists.lazy math math.primes.factors
prettyprint sequences ;
 
Line 336 ⟶ 786:
] lmap-lazy ;
 
15 A005179 ltake list>array .</langsyntaxhighlight>
{{out}}
<pre>
Line 343 ⟶ 793:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#define UPTO 15
 
function divisors(byval n as ulongint) as uinteger
Line 371 ⟶ 821:
next i
print
end</langsyntaxhighlight>
{{out}}<pre> 1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 404 ⟶ 854:
}
fmt.Println(seq)
}</langsyntaxhighlight>
 
{{out}}
Line 414 ⟶ 864:
=={{header|Haskell}}==
Without any subtlety or optimisation, but still fast at this modest scale:
<langsyntaxhighlight lang="haskell">import Data.Numbers.PrimesList (primeFactorsfind, group, sort)
import Data.ListMaybe (find, group, sortmapMaybe)
import Data.MaybeNumbers.Primes (catMaybesprimeFactors)
 
------------------------- A005179 ------------------------
 
a005179 :: [Int]
a005179 =
mapMaybe
catMaybes $
( \n ->
(\n -> find ((n ==) . succ . length . properDivisors) [1 ..]) <$> [1 ..]
find
((n ==) . succ . length . properDivisors)
[1 ..]
)
[1 ..]
 
--------------------------- TEST -------------------------
main :: IO ()
main = print $ take 15 a005179
 
------------------------- GENERIC ------------------------
 
properDivisors :: Int -> [Int]
properDivisors =
init .
sort . sort
. foldr
foldr --
(flip ((<*>) . fmap (*)) . scanl (*) 1)
[1] .
group . primeFactorsgroup
. primeFactors</syntaxhighlight>
 
main :: IO ()
main = print $ take 15 a005179</lang>
{{Out}}
<pre>[1,2,4,6,16,12,64,24,36,48,1024,60,4096,192,144]</pre>
Line 441 ⟶ 902:
Defining a generator in terms of re-usable generic functions, and taking the first 15 terms:
 
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 655 ⟶ 1,116:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>[1,2,4,6,16,12,64,24,36,48,1024,60,4096,192,144]</pre>
Line 662 ⟶ 1,123:
=={{header|J}}==
Rather than factoring by division, this algorithm uses a sieve to tally the factors. Of the whole numbers below ten thousand these are the smallest with n divisors. The list is fully populated through the first 15.
<syntaxhighlight lang="text">
sieve=: 3 :0
range=. <. + i.@:|@:-
Line 672 ⟶ 1,133:
/:~({./.~ {."1) tally,.i.#tally
)
</syntaxhighlight>
</lang>
<pre>
|: sieve 10000
Line 681 ⟶ 1,142:
=={{header|Java}}==
{{trans|C}}
<langsyntaxhighlight lang="java">import java.util.Arrays;
 
public class OEIS_A005179 {
Line 711 ⟶ 1,172:
System.out.println(Arrays.toString(seq));
}
}</langsyntaxhighlight>
 
{{out}}
Line 718 ⟶ 1,179:
[1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144]
</pre>
 
=={{header|jq}}==
This entry uses a streaming approach to avoid constructing unnecessary arrays.
<syntaxhighlight lang="jq"># divisors as an unsorted stream (without calling sqrt)
def divisors:
if . == 1 then 1
else . as $n
| label $out
| range(1; $n) as $i
| ($i * $i) as $i2
| if $i2 > $n then break $out
else if $i2 == $n
then $i
elif ($n % $i) == 0
then $i, ($n/$i)
else empty
end
end
end;
 
def count(s): reduce s as $x (0; .+1);
 
# smallest number with exactly n divisors
def A005179:
. as $n
| first( range(1; infinite) | select( count(divisors) == $n ));
 
# The task:
"The first 15 terms of the sequence are:",
[range(1; 16) | A005179]
</syntaxhighlight>
 
''Invocation'': jq -ncr -f A005179.jq
{{out}}
<pre>The first 15 terms of the sequence are:
[1,2,4,6,16,12,64,24,36,48,1024,60,4096,192,144]</pre>
 
=={{header|Julia}}==
{{works with|Julia|1.2}}
<langsyntaxhighlight lang="julia">using Primes
 
numfactors(n) = reduce(*, e+1 for (_,e) in factor(n); init=1)
Line 729 ⟶ 1,226:
println("The first 15 terms of the sequence are:")
println(map(A005179, 1:15))
</langsyntaxhighlight>{{out}}
<pre>
First 15 terms of OEIS sequence A005179:
Line 737 ⟶ 1,234:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">// Version 1.3.21
 
const val MAX = 15
Line 767 ⟶ 1,264:
}
println(seq.asList())
}</langsyntaxhighlight>
 
{{output}}
Line 776 ⟶ 1,273:
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">
<lang Maple>
with(NumberTheory):
 
Line 791 ⟶ 1,288:
seq(sequenceValue(number), number = 1..15);
 
</syntaxhighlight>
</lang>
{{out}}<pre>
1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144
Line 797 ⟶ 1,294:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Take[SplitBy[SortBy[{DivisorSigma[0, #], #} & /@ Range[100000], First], First][[All, 1]], 15] // Grid</langsyntaxhighlight>
{{out}}
<pre>1 1
Line 814 ⟶ 1,311:
14 192
15 144</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
divisors = function(n)
divs = {1: 1}
divs[n] = 1
i = 2
while i * i <= n
if n % i == 0 then
divs[i] = 1
divs[n / i] = 1
end if
i += 1
end while
return divs.indexes
end function
 
counts = []
for i in range(1, 15)
j = 1
while divisors(j).len != i
j += 1
end while
counts.push(j)
end for
 
print "The first 15 terms in the sequence are:"
print counts.join(", ")
</syntaxhighlight>
{{out}}
<pre>
The first 15 terms in the sequence are:
1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (show (take 15 a005179) ++ "\n")]
 
a005179 :: [num]
a005179 = map smallest_n_divisors [1..]
 
smallest_n_divisors :: num->num
smallest_n_divisors n = hd [i | i<-[1..]; n = #divisors i]
 
divisors :: num->[num]
divisors n = [d | d<-[1..n]; n mod d=0]</syntaxhighlight>
{{out}}
<pre>[1,2,4,6,16,12,64,24,36,48,1024,60,4096,192,144]</pre>
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE A005179;
FROM InOut IMPORT WriteCard, WriteLn;
 
CONST Amount = 15;
VAR found, i, ndivs: CARDINAL;
A: ARRAY [1..Amount] OF CARDINAL;
PROCEDURE divisors(n: CARDINAL): CARDINAL;
VAR count, i: CARDINAL;
BEGIN
count := 0;
i := 1;
WHILE i*i <= n DO
IF n MOD i = 0 THEN
INC(count);
IF n DIV i # i THEN
INC(count);
END;
END;
INC(i);
END;
RETURN count;
END divisors;
 
BEGIN
FOR i := 1 TO Amount DO A[i] := 0; END;
found := 0;
i := 1;
WHILE found < Amount DO
ndivs := divisors(i);
IF (ndivs <= Amount) AND (A[ndivs] = 0) THEN
INC(found);
A[ndivs] := i;
END;
INC(i);
END;
FOR i := 1 TO Amount DO
WriteCard(A[i], 4);
WriteLn;
END;
END A005179.</syntaxhighlight>
{{out}}
<pre> 1
2
4
6
16
12
64
24
36
48
1024
60
4096
192
144</pre>
 
=={{header|Nanoquery}}==
{{trans|Java}}
<langsyntaxhighlight lang="nanoquery">def count_divisors(n)
count = 0
for (i = 1) ((i * i) <= n) (i += 1)
Line 844 ⟶ 1,450:
end
end
println seq</langsyntaxhighlight>
{{out}}
<pre>The first 15 terms of the sequence are:
Line 851 ⟶ 1,457:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="nim">import strformat
 
const MAX = 15
Line 877 ⟶ 1,483:
inc n
inc i
echo sequence</langsyntaxhighlight>
{{out}}
<pre>
Line 886 ⟶ 1,492:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory 'divisors';
Line 896 ⟶ 1,502:
print "$l " and last if $n == divisors($l);
}
}</langsyntaxhighlight>
{{out}}
<pre>First 15 terms of OEIS: A005179
Line 903 ⟶ 1,509:
=={{header|Phix}}==
===naive===
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">15</span>
Line 917 ⟶ 1,523:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first %d terms are: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 926 ⟶ 1,532:
Using the various formula from the OEIS:A005179 link above.<br>
get_primes() and product() have recently been added as new builtins, if necessary see [[Extensible_prime_generator#Phix|Extensible_prime_generator]] and [[Deconvolution/2D%2B#Phix]].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">32</span><span style="color: #0000FF;">?</span><span style="color: #000000;">58</span><span style="color: #0000FF;">:</span><span style="color: #000000;">66</span><span style="color: #0000FF;">)</span>
Line 955 ⟶ 1,561:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d-&gt;%d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
Note: the f[2]>2 test should really be something more like >log(get_primes(-(lf-1))[$])/log(2),
apparently, but everything seems ok within the IEEE 754 53/64 bit limits this imposes.
Line 1,036 ⟶ 1,642:
and adj are not exactly, um, scientific. Completes in about 0.1s
{{libheader|Phix/mpfr}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,158 ⟶ 1,764:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pn</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_free</span><span style="color: #0000FF;">({</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pn</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,206 ⟶ 1,812:
2^k*3^4*p (made up rule):4
1440 (complete fudge):1
</pre>
 
=={{header|PROMAL}}==
Builds a table of divisor counts wihout division.
<syntaxhighlight lang="promal">
;;; Find the smallest number with n divisors, n in 1..15
 
PROGRAM SmallestN
INCLUDE LIBRARY
 
CON WORD maxDivisors = 15 ; number of sequence elements to find
CON WORD maxNUmber = 6000 ; maximum number we will consider
 
WORD dc [ maxNumber + 1 ]
WORD firstWithDivisors [ maxDivisors + 1 ]
WORD found
WORD divisors
WORD d
WORD n
WORD j
 
BEGIN
; compute a table of divisor counts
FOR n = 1 TO maxNumber
dc[ n ] = 0
FOR n = 1 TO maxNumber
j = n
WHILE j <= maxNumber
dc[ j ] = dc[ j ] + 1
j = j + n
; find the first number wih the required divisor counts
FOR n = 0 TO maxDivisors
firstWithDivisors[ n ] = 0
found = 0
n = 0
WHILE found < maxDivisors
n = n + 1
divisors = dc[ n ]
IF divisors <= maxDivisors
IF firstWithDivisors[ divisors ] = 0
; first number with this number of divisors
found = found + 1
firstWithDivisors[ divisors ] = n
FOR d = 1 TO maxDivisors
OUTPUT " #W", firstWithDivisors[ d ]
END
</syntaxhighlight>
{{out}}
<pre>
1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144
</pre>
 
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight Pythonlang="python">def divisors(n):
divs = [1]
for ii in range(2, int(n ** 0.5) + 3):
Line 1,237 ⟶ 1,893:
if __name__ == '__main__':
for item in sequence(15):
print(item)</langsyntaxhighlight>
{{Out}}
<pre>1
Line 1,259 ⟶ 1,915:
 
Not optimized, but still fast at this modest scale.
<langsyntaxhighlight lang="python">'''Smallest number with exactly n divisors'''
 
from itertools import accumulate, chain, count, groupby, islice, product
Line 1,362 ⟶ 2,018:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>[1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144]</pre>
Line 1,369 ⟶ 2,025:
<code>factors</code> is defined at [http://rosettacode.org/wiki/Factors_of_an_integer#Quackery Factors of an integer].
 
<syntaxhighlight lang="quackery"> [ 0
<lang Quackery> [ 0
[ 1+ 2dup
factors size =
Line 1,375 ⟶ 2,031:
nip ] is nfactors ( n --> n )
 
15 times [ i^ 1+ nfactors echo sp ]</langsyntaxhighlight>
 
{{out}}
 
<pre>1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre>
 
=={{header|R}}==
Could probably be speeded up by caching the results of divisorCount, but it's quick enough already. Not to mention, delightfully short.
<syntaxhighlight lang="rsplus">#Need to add 1 to account for skipping n. Not the most efficient way to count divisors, but quite clear.
divisorCount <- function(n) length(Filter(function(x) n %% x == 0, seq_len(n %/% 2))) + 1
smallestWithNDivisors <- function(n)
{
i <- 1
while(divisorCount(i) != n) i <- i + 1
i
}
print(sapply(1:15, smallestWithNDivisors))</syntaxhighlight>
{{out}}
<pre>[1] 1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre>
 
=={{header|Raku}}==
Line 1,385 ⟶ 2,055:
{{works with|Rakudo|2019.03}}
 
<syntaxhighlight lang="raku" perl6line>sub div-count (\x) {
return 2 if x.is-prime;
+flat (1 .. x.sqrt.floor).map: -> \d {
Line 1,397 ⟶ 2,067:
put (1..$limit).map: -> $n { first { $n == .&div-count }, 1..Inf };
 
</syntaxhighlight>
</lang>
{{out}}
<pre>First 15 terms of OEIS:A005179
Line 1,404 ⟶ 2,074:
=={{header|REXX}}==
===some optimization===
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays the smallest number with exactly N divisors.*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/
Line 1,437 ⟶ 2,107:
else if k*k>x then leave /*only divide up to √ x */
end /*k*/ /* [↑] this form of DO loop is faster.*/
return #+1 /*bump "proper divisors" to "divisors".*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,459 ⟶ 2,129:
 
===with memorization===
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays the smallest number with exactly N divisors.*/
parse arg N lim . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/
Line 1,494 ⟶ 2,164:
else if k*k>x then leave /*only divide up to √ x */
end /*k*/ /* [↑] this form of DO loop is faster.*/
return #+1 /*bump "proper divisors" to "divisors".*/</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,520 ⟶ 2,190:
 
see nl + "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,528 ⟶ 2,198:
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49g}}
≪ {1}
2 ROT '''FOR''' j
1
'''DO''' 1 + '''UNTIL''' DUP DIVIS SIZE j == '''END'''
+
'''NEXT '''
≫ '<span style="color:blue">TASK</span>' STO
 
15 <span style="color:blue">TASK</span>
{{out}}
<pre>
1: {1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144}
</pre>
Runs in 13 minutes on an HP-50g.
====Faster implementation====
With the hint given in the [https://oeis.org/A005179 OEIS page] that a(p)=2^(p-1) when p is prime:
≪ {1}
2 ROT '''FOR''' j
'''IF''' j ISPRIME? '''THEN''' 2 j 1 - ^
'''ELSE'''
1 '''DO''' 1 + '''UNTIL''' DUP DIVIS SIZE j == '''END'''
'''END'''
+
'''NEXT '''
≫ '<span style="color:blue">TASK</span>' STO
the same result is obtained in less than a minute.
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'prime'
def num_divisors(n)
Line 1,541 ⟶ 2,240:
 
p (1..15).map{|n| first_with_num_divs(n) }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,548 ⟶ 2,247:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
use itertools::Itertools;
 
Line 1,718 ⟶ 2,417:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,788 ⟶ 2,487:
64 7560
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program smallest_number_with_exactly_n_divisors;
print([a(n) : n in [1..15]]);
 
proc a(n);
return [i +:= 1 : until n = #[d : d in [1..i] | i mod d=0]](i);
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>[1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144]</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func n_divisors(n) {
1..Inf -> first_by { .sigma0 == n }
}
 
say 15.of { n_divisors(_+1) }</langsyntaxhighlight>
 
{{out}}
Line 1,802 ⟶ 2,512:
 
More efficient solution:
<langsyntaxhighlight lang="ruby">func n_divisors(threshold, least_solution = Inf, k = 1, max_a = Inf, solutions = 1, n = 1) {
 
if (solutions == threshold) {
Line 1,824 ⟶ 2,534:
 
say n_divisors(60) #=> 5040
say n_divisors(1000) #=> 810810000</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">// See https://en.wikipedia.org/wiki/Divisor_function
func divisorCount(number: Int) -> Int {
var n = number
Line 1,869 ⟶ 2,579:
print(n, terminator: " ")
}
print()</langsyntaxhighlight>
 
{{out}}
Line 1,877 ⟶ 2,587:
 
=={{header|Tcl}}==
<langsyntaxhighlight Tcllang="tcl">proc divCount {n} {
set cnt 0
for {set d 1} {($d * $d) <= $n} {incr d} {
Line 1,910 ⟶ 2,620:
 
show A005179 1 15
</syntaxhighlight>
</lang>
{{out}}
<pre>A005179(1..15) =
Line 1,917 ⟶ 2,627:
=={{header|Wren}}==
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
 
var limit = 22
Line 1,931 ⟶ 2,641:
}
System.print("The first %(limit) terms are:")
System.print(numbers)</langsyntaxhighlight>
 
{{out}}
Line 1,937 ⟶ 2,647:
The first 22 terms are:
[1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144, 120, 65536, 180, 262144, 240, 576, 3072]
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func Divisors(N); \Return number of divisors of N
int N, Count, D;
[Count:= 0;
for D:= 1 to N do
if rem(N/D) = 0 then Count:= Count+1;
return Count;
];
 
int N, AN;
[for N:= 1 to 15 do
[AN:= 0;
repeat AN:= AN+1 until Divisors(AN) = N;
IntOut(0, AN); ChOut(0, ^ );
];
]</syntaxhighlight>
 
{{out}}
<pre>
1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn countDivisors(n)
{ [1.. n.toFloat().sqrt()].reduce('wrap(s,i){ s + (if(0==n%i) 1 + (i!=n/i)) },0) }
A005179w:=(1).walker(*).tweak(fcn(n){
Line 1,949 ⟶ 2,681:
if(n<d and not cache.find(d)) cache[d]=N;
}
});</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">N:=15;
println("First %d terms of OEIS:A005179".fmt(N));
A005179w.walk(N).concat(" ").println();</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits