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

m (→‎more optimization: fixed a typo.)
Line 613:
<pre>First 20 terms of OEIS:A073916
1 3 25 14 14641 44 24137569 70 1089 405 819628286980801 160 22563490300366186081 2752 9801 462 21559177407076402401757871041 1044 740195513856780056217081017732809 1520</pre>
 
=={{header|Phix}}==
Certainly not the fastest way to do it, hence the relatively small limit of 24, which takes less than 0.4s,<br>
whereas a limit of 25 would need to invoke factors() 52 million times which would no doubt take a fair while.
<lang Phix>constant LIMIT = 24
include mpfr.e
mpz z = mpz_init()
 
sequence fn = 1&repeat(0,LIMIT-1),
primes = {2,3}
integer k = 1
printf(1,"The first %d terms in the sequence are:\n",LIMIT)
for i=1 to LIMIT do
sequence f = factors(i,1)
if length(f)=2 then -- i is prime (f is {1,i})
while length(primes)<i do
integer p = primes[$]+2
while prime_factors(p)!={} do p += 2 end while
primes = append(primes,p)
end while
mpz_ui_pow_ui(z,primes[i],i-1)
printf(1,"%2d : %s\n",{i,mpz_get_str(z)})
else
while fn[i]<i do
k += 1
integer l = length(factors(k,1))
if l<=LIMIT and fn[l]<l then
fn[l] = iff(fn[l]+1<l?fn[l]+1:k)
end if
end while
printf(1,"%2d : %d\n",{i,fn[i]})
end if
end for</lang>
{{out}}
<pre>
The first 24 terms in the sequence are:
1 : 1
2 : 3
3 : 25
4 : 14
5 : 14641
6 : 44
7 : 24137569
8 : 70
9 : 1089
10 : 405
11 : 819628286980801
12 : 160
13 : 22563490300366186081
14 : 2752
15 : 9801
16 : 462
17 : 21559177407076402401757871041
18 : 1044
19 : 740195513856780056217081017732809
20 : 1520
21 : 141376
22 : 84992
23 : 1658509762573818415340429240403156732495289
24 : 1170
</pre>
 
=={{header|REXX}}==
7,794

edits