Jump to content

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

(Add Factor example)
Line 388:
 
=={{header|Phix}}==
Uses primes[] and add_block() from [[Extensible_prime_generator#Phix|Extensible_prime_generator]]<br>
<lang Phix>constant limit = 15
product() has recently been added as a new builtin, if you need it see [[Deconvolution/2D%2B#Phix]].
sequence res = repeat(0,limit)
<lang Phix>constant limit = iff(machine_bits()=32?58:66)
integer found = 0, n = 1
sequence found = repeat(0,limit)
while found<limit do
integer kn = length(factors(n,1))
atom ri
if k<=limit and res[k]=0 then
for i=1 to limit do
res[k] = n
sequence f found += factors(i,1)
integer lf = length(f)
if lf<=2 then
ri = power(2,i-1) -- prime (or 1)
elsif lf=3 then
ri = power(6,f[2]-1) -- p^2 (eg f={1,5,25})
elsif f[2]>2 -- (see note)
and f[$] = power(f[2],lf-1) then
while length(primes)<lf-1 do
add_block()
end while
ri = power(product(primes[1..lf-1]),f[2]-1) -- p^k (eg f={1,3,9,27})
elsif length(f)=4 then
ri = power(2,f[3]-1)*power(3,f[2]-1) -- p*q (eg f={1,2,3,6})
else
while found[i]=0 do
integer k = length(factors(n,1)) -- do the rest manually
if k<=limit and found[k]=0 then
found[k] = n
end if
n += 1
end while
ri = found[i]
end if
printf(1,"%d->%d\n",{i,ri})
n += 1
end whilefor</lang>
note: the f[2]>2 test should really be something more like >log(primes[lf-1])/log(2),
printf(1,"The first %d terms are: %v\n",{limit,res})</lang>
apparently, but everything seems ok within the IEE754 53/64 bit limits this imposes.
afaict, it takes longer to print the answers that it did to calculate them, tee hee!
{{out}}
64-bit (as shown) manages 8 more answers than 32-bit, which as per limit halts on 58.
<pre>
1->1
The first 15 terms are: {1,2,4,6,16,12,64,24,36,48,1024,60,4096,192,144}
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->900
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
65->331776
66->46080
</pre>
You would need something quite a bit smarter to venture over a limit of 28.
 
=={{header|REXX}}==
7,820

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.