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

Content added Content deleted
Line 388: Line 388:


=={{header|Phix}}==
=={{header|Phix}}==
Using the formulae from the OEIS:A005179 link above.<br>
Using the various formula from the OEIS:A005179 link above.<br>
Uses primes[] and add_block() from [[Extensible_prime_generator#Phix|Extensible_prime_generator]],
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]].
product() has recently been added as a new builtin, if you need it see [[Deconvolution/2D%2B#Phix]].
<lang Phix>constant limit = iff(machine_bits()=32?58:66)
<lang Phix>constant limit = iff(machine_bits()=32?58:66)
sequence found = repeat(0,limit)
sequence found = repeat(0,limit)
integer n = 1
integer n = 1

atom ri
procedure populate_found(integer i)
while found[i]=0 do
integer k = length(factors(n,1))
if k<=limit and found[k]=0 then
found[k] = n
end if
n += 1
end while
end procedure

for i=1 to limit do
for i=1 to limit do
sequence f = factors(i,1)
sequence f = factors(i,1)
integer lf = length(f)
integer lf = length(f)
if lf<=2 then
atom ri
ri = power(2,i-1) -- prime (or 1)
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 lf=3 then
ri = power(6,f[2]-1) -- p^2 (eg f={1,5,25})
elsif f[2]>2 -- (see note)
elsif f[2]>2 -- (see note)
and f[$] = power(f[2],lf-1) then
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})
elsif length(f)=4 then ri = power(2,f[3]-1)*power(3,f[2]-1) -- p*q (eg f={1,2,3,6})
while length(primes)<lf-1 do
else populate_found(i) ri = found[i] -- do the rest manually
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
end if
printf(1,"%d->%d\n",{i,ri})
printf(1,"%d->%d\n",{i,ri})
end for</lang>
end for</lang>
note: the f[2]>2 test should really be something more like >log(primes[lf-1])/log(2),
Note: the f[2]>2 test should really be something more like >log(primes[lf-1])/log(2),
apparently, but everything seems ok within the IEE754 53/64 bit limits this imposes.
apparently, but everything seems ok within the IEEE 754 53/64 bit limits this imposes.
afaict, it takes longer to print the answers that it did to calculate them, tee hee!
It takes longer, afaict, to print the answers that it did to calculate them, tee hee!
{{out}}
{{out}}
64-bit (as shown) manages 8 more answers than 32-bit, which as per limit halts on 58.
64-bit (as shown) manages 8 more answers than 32-bit, which as per limit halts on 58.