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

m
(add FreeBASIC)
m (→‎{{header|Wren}}: Minor tidy)
 
(31 intermediate revisions by 20 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
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Take[SplitBy[SortBy[{DivisorSigma[0, #], #} & /@ Range[100000], First], First][[All, 1]], 15] // Grid</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</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 825 ⟶ 1,450:
end
end
println seq</langsyntaxhighlight>
{{out}}
<pre>The first 15 terms of the sequence are:
Line 832 ⟶ 1,457:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="nim">import strformat
 
const MAX = 15
Line 858 ⟶ 1,483:
inc n
inc i
echo $sequence</langsyntaxhighlight>
{{out}}
<pre>
Line 867 ⟶ 1,492:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory 'divisors';
Line 877 ⟶ 1,502:
print "$l " and last if $n == divisors($l);
}
}</langsyntaxhighlight>
{{out}}
<pre>First 15 terms of OEIS: A005179
Line 884 ⟶ 1,509:
=={{header|Phix}}==
===naive===
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant limit = 15
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
sequence res = repeat(0,limit)
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">15</span>
integer found = 0, n = 1
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span>
while found<limit do
<span style="color: #004080;">integer</span> <span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
integer k = length(factors(n,1))
<span style="color: #008080;">while</span> <span style="color: #000000;">found</span><span style="color: #0000FF;"><</span><span style="color: #000000;">limit</span> <span style="color: #008080;">do</span>
if k<=limit and res[k]=0 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
res[k] = n
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">limit</span> <span style="color: #008080;">and</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
found += 1
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
end if
<span style="color: #000000;">found</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
n += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
printf(1,"The first %d terms are: %v\n",{limit,res})</lang>
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 904 ⟶ 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]].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant limit = iff(machine_bits()=32?58:66)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
sequence found = repeat(0,limit)
<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>
integer n = 1
<span style="color: #004080;">sequence</span> <span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span>
 
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
procedure populate_found(integer i)
while found[i]=0 do
<span style="color: #008080;">procedure</span> <span style="color: #000000;">populate_found</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
integer k = length(factors(n,1))
<span style="color: #008080;">while</span> <span style="color: #000000;">found</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
if k<=limit and found[k]=0 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
found[k] = n
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">limit</span> <span style="color: #008080;">and</span> <span style="color: #000000;">found</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">found</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
n += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
for i=1 to limit do
sequence f = factors(i,1)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">do</span>
integer lf = length(f)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
atom ri
<span style="color: #004080;">integer</span> <span style="color: #000000;">lf</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
if lf<=2 then ri = power(2,i-1) -- prime (or 1)
<span style="color: #004080;">atom</span> <span style="color: #000000;">ri</span>
elsif lf=3 then ri = power(6,f[2]-1) -- p^2 (eg f={1,5,25})
<span style="color: #008080;">if</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span> <span style="color: #000000;">ri</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- prime (or 1)</span>
elsif f[2]>2 -- (see note)
<span style="color: #008080;">elsif</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">then</span> <span style="color: #000000;">ri</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- p^2 (eg f={1,5,25})</span>
and f[$] = power(f[2],lf-1) then ri = power(product(get_primes(-(lf-1))),f[2]-1) -- p^k (eg f={1,3,9,27})
<span style="color: #008080;">elsif</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]></span><span style="color: #000000;">2</span> <span style="color: #000080;font-style:italic;">-- (see note)</span>
elsif lf=4 then ri = power(2,f[3]-1)*power(3,f[2]-1) -- p*q (eg f={1,2,3,6})
<span style="color: #008080;">and</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">[$]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #000000;">lf</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">ri</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">product</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes</span><span style="color: #0000FF;">(-(</span><span style="color: #000000;">lf</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))),</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- p^k (eg f={1,3,9,27})</span>
else populate_found(i) ri = found[i] -- do the rest manually
<span style="color: #008080;">elsif</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span> <span style="color: #008080;">then</span> <span style="color: #000000;">ri</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- p*q (eg f={1,2,3,6})</span>
end if
<span style="color: #008080;">else</span> <span style="color: #000000;">populate_found</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: #000000;">found</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- do the rest manually</span>
printf(1,"%d->%d\n",{i,ri})
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for</lang>
<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>
<!--</syntaxhighlight>-->
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,011 ⟶ 1,642:
and adj are not exactly, um, scientific. Completes in about 0.1s
{{libheader|Phix/mpfr}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include mpfr.e
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
mpz r = mpz_init(),
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
pn = mpz_init()
<span style="color: #004080;">mpz</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(),</span>
sequence rule_names = {},
<span style="color: #000000;">pn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
rule_counts = {}
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rule_names</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
for i=1 to 2000 do
<span style="color: #000000;">rule_counts</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
sequence pf = prime_factors(i,true), ri, adj
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2000</span> <span style="color: #008080;">do</span>
integer lf = length(pf), np, p2, p3, p5, p, e
<span style="color: #004080;">sequence</span> <span style="color: #000000;">pf</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prime_factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">ri</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">adj</span>
string what
<span style="color: #004080;">integer</span> <span style="color: #000000;">lf</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pf</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">np</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">e</span>
if lf>10 then ?9/0 end if
<span style="color: #004080;">string</span> <span style="color: #000000;">what</span>
if lf<=1 then what = "prime (proper rule)"
<span style="color: #008080;">if</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">></span><span style="color: #000000;">10</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
np = 1
<span style="color: #008080;">if</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000000;">what</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"prime (proper rule)"</span>
adj = {i}
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
elsif pf[$]=2 then what = "2^k (made up rule)"
<span style="color: #000000;">adj</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">}</span>
np = lf-1
<span style="color: #008080;">elsif</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$]=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span> <span style="color: #000000;">what</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"2^k (made up rule)"</span>
p2 = {2,4,4,4,4,4,4,8,8}[np]
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
p3 = {2,2,2,2,4,4,4,4,4}[np]
<span style="color: #000000;">p2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">np</span><span style="color: #0000FF;">]</span>
np = {2,2,3,4,4,5,6,6,7}[np]
<span style="color: #000000;">p3</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">np</span><span style="color: #0000FF;">]</span>
adj = {p2,p3}
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">np</span><span style="color: #0000FF;">]</span>
elsif pf[$]=3
<span style="color: #000000;">adj</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">}</span>
and pf[$-1]=2 then what = "2^k*3 (made up rule)"
<span style="color: #008080;">elsif</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$]=</span><span style="color: #000000;">3</span>
np = lf-1
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span> <span style="color: #000000;">what</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"2^k*3 (made up rule)"</span>
p2 = {3,3,4,4,4,6,6,6,6}[np]
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
p3 = {2,2,3,3,3,4,4,4,4}[np]
<span style="color: #000000;">p2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">np</span><span style="color: #0000FF;">]</span>
np = {2,3,3,4,5,5,6,7,8}[np]
<span style="color: #000000;">p3</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">np</span><span style="color: #0000FF;">]</span>
adj = {p2,p3}
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">np</span><span style="color: #0000FF;">]</span>
elsif lf>4
<span style="color: #000000;">adj</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">}</span>
and pf[$-1]=2 then what="2^k*p (made up rule)"
<span style="color: #008080;">elsif</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">></span><span style="color: #000000;">4</span>
np = lf-1
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span> <span style="color: #000000;">what</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"2^k*p (made up rule)"</span>
adj = {0,4}
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
elsif lf>4
<span style="color: #000000;">adj</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">}</span>
and pf[$]=3
<span style="color: #008080;">elsif</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">></span><span style="color: #000000;">4</span>
and pf[$-1]=3
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$]=</span><span style="color: #000000;">3</span>
and pf[$-2]=2 then what="2^k*3^2*p (made up rule)"
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">3</span>
np = lf-4
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span> <span style="color: #000000;">what</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"2^k*3^2*p (made up rule)"</span>
p3 = {3,3,3,4,4}[np]
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">-</span><span style="color: #000000;">4</span>
p5 = {2,2,2,3,3}[np]
<span style="color: #000000;">p3</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">np</span><span style="color: #0000FF;">]</span>
np = {4,5,6,6,7}[np]
<span style="color: #000000;">p5</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">np</span><span style="color: #0000FF;">]</span>
adj = {6,p3,p5}
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">np</span><span style="color: #0000FF;">]</span>
elsif lf>4
<span style="color: #000000;">adj</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p5</span><span style="color: #0000FF;">}</span>
and pf[$]=3
<span style="color: #008080;">elsif</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">></span><span style="color: #000000;">4</span>
and pf[$-2]=3
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$]=</span><span style="color: #000000;">3</span>
and pf[$-4]=2 then what="2^k*3^3*p (made up rule)"
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">3</span>
np = lf-1
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span> <span style="color: #000000;">what</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"2^k*3^3*p (made up rule)"</span>
adj = {6}
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
elsif lf>5
<span style="color: #000000;">adj</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">}</span>
and pf[$]>3
<span style="color: #008080;">elsif</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">></span><span style="color: #000000;">5</span>
and pf[$-1]=3
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$]></span><span style="color: #000000;">3</span>
and pf[$-4]=3
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">3</span>
and pf[2]=3
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">3</span>
and (pf[1]=2 or pf[$]>5) then what="2^k*3^4*p (made up rule)"
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">3</span>
np = lf
<span style="color: #008080;">and</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">pf</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">2</span> <span style="color: #008080;">or</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$]></span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">what</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"2^k*3^4*p (made up rule)"</span>
adj = {}
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lf</span>
elsif lf>4
<span style="color: #000000;">adj</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
and pf[$-1]=3
<span style="color: #008080;">elsif</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">></span><span style="color: #000000;">4</span>
and pf[$-4]=3
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">3</span>
and (lf>5 or pf[$]=3) then what="[2^k]*3^(>=4)*p (made up rule)"
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">3</span>
np = lf-1
<span style="color: #008080;">and</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">lf</span><span style="color: #0000FF;">></span><span style="color: #000000;">5</span> <span style="color: #008080;">or</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$]=</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">what</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"[2^k]*3^(&gt;=4)*p (made up rule)"</span>
adj = {9,pf[$]}&reverse(pf[1..$-3]) -- <bsg>
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
elsif lf>=7
<span style="color: #000000;">adj</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pf</span><span style="color: #0000FF;">[$]}&</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pf</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">])</span> <span style="color: #000080;font-style:italic;">-- &lt;bsg&gt;</span>
and pf[$]>3
<span style="color: #008080;">elsif</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">7</span>
and pf[$-1]=3
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$]></span><span style="color: #000000;">3</span>
and pf[$-2]=2 then what="2^k*3*p (made up rule)"
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">3</span>
np = lf-1
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span> <span style="color: #000000;">what</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"2^k*3*p (made up rule)"</span>
adj = {0,4,3}
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
elsif i=1440
<span style="color: #000000;">adj</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}</span>
and pf={2,2,2,2,2,3,3,5} then what="1440 (complete fudge)"
<span style="color: #008080;">elsif</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1440</span>
-- nothing quite like this, nothing to build any pattern from...
<span style="color: #008080;">and</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">={</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">then</span> <span style="color: #000000;">what</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"1440 (complete fudge)"</span>
np = 7
<span style="color: #000080;font-style:italic;">-- nothing quite like this, nothing to build any pattern from...</span>
adj = {6,5,3,2,2,2,2}
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">7</span>
else what="general (proper rule)"
<span style="color: #000000;">adj</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}</span>
-- (note this incorporates the p^2, (p>2)^k, p*q, and p*m*q rules)
<span style="color: #008080;">else</span> <span style="color: #000000;">what</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"general (proper rule)"</span>
np = lf
<span style="color: #000080;font-style:italic;">-- (note this incorporates the p^2, (p&gt;2)^k, p*q, and p*m*q rules)</span>
adj = {}
<span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lf</span>
end if
<span style="color: #000000;">adj</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
ri = get_primes(-np)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
for j=1 to length(adj) do
<span style="color: #000000;">ri</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_primes</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">np</span><span style="color: #0000FF;">)</span>
integer aj = adj[j]
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">adj</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if aj!=0 then pf[-j] = aj end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">aj</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">adj</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span>
end for
<span style="color: #008080;">if</span> <span style="color: #000000;">aj</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">pf</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">aj</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
for j=1 to np do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
ri[j] = {ri[j],pf[-j]-1}
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">np</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">ri</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #000000;">pf</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
string short = "" -- (eg "2^2*3^3" form)
mpz_set_si(r,1) -- (above as big int)
<span style="color: #004080;">string</span> <span style="color: #000000;">short_form</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> <span style="color: #000080;font-style:italic;">-- (eg "2^2*3^3" form)</span>
for j=1 to length(ri) do
<span style="color: #7060A8;">mpz_set_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (above as big int)</span>
{p, e} = ri[j]
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if length(short) then short &= "*" end if
<span style="color: #0000FF;">{</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ri</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span>
short &= sprintf("%d",p)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">short_form</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">short_form</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">"*"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if e!=1 then
<span style="color: #000000;">short_form</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
short &= sprintf("^%d",{e})
<span style="color: #008080;">if</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">short_form</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"^%d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">e</span><span style="color: #0000FF;">})</span>
mpz_ui_pow_ui(pn,p,e)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
mpz_mul(r,r,pn)
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</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>
if i<=15 or remainder(i-1,250)>=248 or i=1440 then
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
string rs = mpz_get_str(r)
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">15</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">250</span><span style="color: #0000FF;">)>=</span><span style="color: #000000;">248</span> <span style="color: #008080;">or</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1440</span> <span style="color: #008080;">then</span>
if length(rs)>20 then
<span style="color: #004080;">string</span> <span style="color: #000000;">rs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)</span>
rs[6..-6] = sprintf("<-- %d digits -->",length(rs)-10)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rs</span><span style="color: #0000FF;">)></span><span style="color: #000000;">20</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">rs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">6</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">6</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"&lt;-- %d digits --&gt;"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rs</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
if short="2^0" then short = "1" end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"%4d : %25s %30s %s\n",{i,short,rs,what})
<span style="color: #008080;">if</span> <span style="color: #000000;">short_form</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"2^0"</span> <span style="color: #008080;">then</span> <span style="color: #000000;">short_form</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"1"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<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;">"%4d : %25s %30s %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">short_form</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">what</span><span style="color: #0000FF;">})</span>
integer k = find(what,rule_names)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if k=0 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">what</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rule_names</span><span style="color: #0000FF;">)</span>
rule_names = append(rule_names,what)
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
rule_counts = append(rule_counts,1)
<span style="color: #000000;">rule_names</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rule_names</span><span style="color: #0000FF;">,</span><span style="color: #000000;">what</span><span style="color: #0000FF;">)</span>
else
<span style="color: #000000;">rule_counts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rule_counts</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
rule_counts[k] += 1
<span style="color: #008080;">else</span>
end if
<span style="color: #000000;">rule_counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
integer lr = length(rule_names)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"\nrules(%d):\n",lr)
<span style="color: #004080;">integer</span> <span style="color: #000000;">lr</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rule_names</span><span style="color: #0000FF;">)</span>
sequence tags = custom_sort(rule_counts, tagset(lr))
<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;">"\nrules(%d):\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lr</span><span style="color: #0000FF;">)</span>
for i=1 to lr do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tags</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">custom_sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rule_counts</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lr</span><span style="color: #0000FF;">))</span>
integer ti = tags[-i]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">lr</span> <span style="color: #008080;">do</span>
printf(1," %30s:%d\n",{rule_names[ti],rule_counts[ti]})
<span style="color: #004080;">integer</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tags</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
end for
<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;">" %30s:%d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">rule_names</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">],</span><span style="color: #000000;">rule_counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">]})</span>
{r,pn} = mpz_free({r,pn})</lang>
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,178 ⟶ 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,209 ⟶ 1,893:
if __name__ == '__main__':
for item in sequence(15):
print(item)</langsyntaxhighlight>
{{Out}}
<pre>1
Line 1,231 ⟶ 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,334 ⟶ 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>
 
=={{header|Quackery}}==
<code>factors</code> is defined at [http://rosettacode.org/wiki/Factors_of_an_integer#Quackery Factors of an integer].
 
<syntaxhighlight lang="quackery"> [ 0
[ 1+ 2dup
factors size =
until ]
nip ] is nfactors ( n --> n )
 
15 times [ i^ 1+ nfactors echo sp ]</syntaxhighlight>
 
{{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,342 ⟶ 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,354 ⟶ 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,361 ⟶ 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,394 ⟶ 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,416 ⟶ 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,451 ⟶ 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,477 ⟶ 2,190:
 
see nl + "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,485 ⟶ 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,498 ⟶ 2,240:
 
p (1..15).map{|n| first_with_num_divs(n) }
</syntaxhighlight>
</lang>
{{out}}
<pre>
[1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144]
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
use itertools::Itertools;
 
const PRIMES: [u64; 15] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47];
const MAX_DIVISOR: usize = 64;
 
struct DivisorSeq {
max_number_of_divisors: u64,
index: u64,
}
 
impl DivisorSeq {
fn new(max_number_of_divisors: u64) -> DivisorSeq {
DivisorSeq {
max_number_of_divisors,
index: 1,
}
}
}
 
impl Iterator for DivisorSeq {
type Item = u64;
 
fn next(&mut self) -> Option<u64> {
if self.max_number_of_divisors < self.index {
return None;
}
#[allow(unused_mut)]
let mut result: u64;
let divisors_of_divisor = get_divisors(self.index);
match divisors_of_divisor.len() {
1 | 2 => {
// when # divisors is a prime
result = 2_u64.pow(self.index as u32 - 1);
self.index += 1;
}
3 => {
// when # divisors is a prime square
result = 6_u64.pow(divisors_of_divisor[1] as u32 - 1);
self.index += 1;
}
4 => {
// when # divisors is a prime * non-prime
result = 2_u64.pow(divisors_of_divisor[2] as u32 - 1)
* 3_u64.pow(divisors_of_divisor[1] as u32 - 1);
self.index += 1;
}
8 if divisors_of_divisor
.iter()
.filter(|x| PRIMES.contains(x))
.count()
== 3 =>
{
// sphenic numbers, aka p*m*q, where, p, m and q are prime
let first_primes = divisors_of_divisor
.iter()
.filter(|x| PRIMES.contains(x))
.collect::<Vec<_>>();
result = 2_u64.pow(*first_primes[2] as u32 - 1)
* 3_u64.pow(*first_primes[1] as u32 - 1)
* 5_u64.pow(*first_primes[0] as u32 - 1);
self.index += 1;
}
_ => {
// brute force and slow: iterates over the numbers to find
// one with the appropriate number of divisors
let mut x: u64 = 1;
loop {
if get_divisors(x).len() as u64 == self.index {
break;
}
x += 1;
}
 
result = x;
self.index += 1;
}
}
Some(result)
}
}
/// Gets all divisors of a number
fn get_divisors(n: u64) -> Vec<u64> {
let mut results = Vec::new();
 
for i in 1..(n / 2 + 1) {
if n % i == 0 {
results.push(i);
}
}
results.push(n);
results
}
 
fn main() {
// simple version using factorizing numbers
// with rules applied from A005179 so speed up
// but as such it is slow in some cases, e.g for 52
let seq_iter = DivisorSeq::new(64);
println!("Simple method with rules");
println!("# divisors Smallest number");
for (i, x) in seq_iter.enumerate() {
println!("{:>10}{:20}", i + 1, x);
}
 
// more advanced version using calculations based on number of
// prime factors and their exponent
 
// load initial result table with an initial value of 2**n for each item
let mut min_numbers = vec![0_u64; MAX_DIVISOR];
(0_usize..MAX_DIVISOR).for_each(|n| min_numbers[n] = 2_u64.pow(n as u32));
 
let prime_list = (1..15).map(|i| PRIMES[0..=i].to_vec()).collect::<Vec<_>>();
 
for pl in prime_list.iter() {
// calculate the max exponent a prime can get in a given prime-list
// to be able to produce the desired number of divisors
let max_exponent = 1 + MAX_DIVISOR as u32 / 2_u32.pow(pl.len() as u32 - 1);
 
// create a combination of exponents using cartesian product
let exponents = (1_usize..=pl.len())
.map(|_| 1_u32..=max_exponent)
.multi_cartesian_product()
.filter(|elt| {
let mut prev = None::<&u32>;
elt.iter().all(|x| match prev {
Some(n) if x > n => false,
_ => {
prev = Some(x);
true
}
})
});
 
// iterate throught he exponent combinations
for exp in exponents {
// calculate the number of divisors using the formula
// given primes of p, q, r
// and exponents of a1, a2, a3
// the # divisors is (a1+1)* (a2+1) *(a3+1)
let num_of_divisors = exp.iter().map(|x| x + 1).product::<u32>() as usize;
 
// and calculate the number with those primes and the given exponent set
let num = pl.iter().zip(exp.iter()).fold(1_u64, |mut acc, (p, e)| {
// check for overflow if numbers won't fit into u64
acc = match acc.checked_mul(p.pow(*e)) {
Some(z) => z,
_ => 0,
};
acc
});
 
// finally, if the number is less than what we have so far in the result table
// replace the result table with the smaller number
if num > 0
&& min_numbers.len() >= num_of_divisors
&& min_numbers[num_of_divisors - 1] > num
{
min_numbers[num_of_divisors - 1] = num;
}
}
}
 
println!("Advanced method");
println!("# divisors Smallest number");
for (i, x) in min_numbers.iter().enumerate() {
println!("{:>10}{:20}", i + 1, x);
}
}
 
</syntaxhighlight>
{{out}}
<pre>
Simple method with rules
# divisors Smallest number
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
16 120
17 65536
18 180
19 262144
20 240
21 576
22 3072
23 4194304
24 360
25 1296
26 12288
27 2304
28 960
29 268435456
30 720
31 1073741824
32 840
33 9216
34 196608
35 5184
36 1260
37 68719476736
38 786432
39 36864
40 1680
41 1099511627776
42 2880
43 4398046511104
44 15360
45 3600
46 12582912
47 70368744177664
48 2520
49 46656
50 6480
51 589824
52 61440
53 4503599627370496
54 6300
55 82944
56 6720
57 2359296
58 805306368
59 288230376151711744
60 5040
61 1152921504606846976
62 3221225472
63 14400
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,515 ⟶ 2,510:
[1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144]
</pre>
 
More efficient solution:
<syntaxhighlight lang="ruby">func n_divisors(threshold, least_solution = Inf, k = 1, max_a = Inf, solutions = 1, n = 1) {
 
if (solutions == threshold) {
return n
}
 
if (solutions > threshold) {
return least_solution
}
 
var p = k.prime
 
for a in (1 .. max_a) {
n *= p
break if (n > least_solution)
least_solution = __FUNC__(threshold, least_solution, k+1, a, solutions * (a + 1), n)
}
 
return least_solution
}
 
say n_divisors(60) #=> 5040
say n_divisors(1000) #=> 810810000</syntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">// See https://en.wikipedia.org/wiki/Divisor_function
func divisorCount(number: Int) -> Int {
var n = number
Line 1,559 ⟶ 2,579:
print(n, terminator: " ")
}
print()</langsyntaxhighlight>
 
{{out}}
Line 1,567 ⟶ 2,587:
 
=={{header|Tcl}}==
<langsyntaxhighlight Tcllang="tcl">proc divCount {n} {
set cnt 0
for {set d 1} {($d * $d) <= $n} {incr d} {
Line 1,600 ⟶ 2,620:
 
show A005179 1 15
</syntaxhighlight>
</lang>
{{out}}
<pre>A005179(1..15) =
Line 1,607 ⟶ 2,627:
=={{header|Wren}}==
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
 
var limit = 22
Line 1,621 ⟶ 2,641:
}
System.print("The first %(limit) terms are:")
System.print(numbers)</langsyntaxhighlight>
 
{{out}}
Line 1,627 ⟶ 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,639 ⟶ 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