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

From Rosetta Code
Content added Content deleted
(Draft task A073916, split off from anti-primes plus)
 
(Move Perl 6 entry from Anti-primes plus task)
Line 7: Line 7:


Show here, on this page, at least the first 10 terms of the sequence.
Show here, on this page, at least the first 10 terms of the sequence.

=={{header|Perl 6}}==
{{works with|Rakudo|2019.03}}

<lang perl6>sub div-count (\x) {
return 2 if x.is-prime;
+flat (1 .. x.sqrt.floor).map: -> \d {
unless x % d { my \y = x div d; y == d ?? y !! (y, d) }
}
}

my $limit = 15;

my @primes = grep { .is-prime }, 1..*;
@primes[$limit]; # prime the array. SCNR

put "\nFirst 15 terms of OEIS:A073916";
put (1..$limit).hyper(:2batch).map: -> $n {
($n > 4 and $n.is-prime) ??
exp($n - 1, @primes[$n - 1]) !!
do {
my $i = 0;
my $iterator = $n %% 2 ?? (1..*) !! (1..*).map: *²;
$iterator.first: {
next unless $n == .&div-count;
next unless ++$i == $n;
$_
}
}
};</lang>

<pre>First 15 terms of OEIS:A073916
1 3 25 14 14641 44 24137569 70 1089 405 819628286980801 160 22563490300366186081 2752 9801</pre>

Revision as of 15:24, 11 April 2019

Sequence: nth number with exactly n divisors is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Calculate the sequence where each term an is the nth that has n divisors.

See OEIS:A073916

Task

Show here, on this page, at least the first 10 terms of the sequence.

Perl 6

Works with: Rakudo version 2019.03

<lang perl6>sub div-count (\x) {

   return 2 if x.is-prime;
   +flat (1 .. x.sqrt.floor).map: -> \d {
       unless x % d { my \y = x div d; y == d ?? y !! (y, d) }
   }

}

my $limit = 15;

my @primes = grep { .is-prime }, 1..*; @primes[$limit]; # prime the array. SCNR

put "\nFirst 15 terms of OEIS:A073916"; put (1..$limit).hyper(:2batch).map: -> $n {

   ($n > 4 and $n.is-prime) ??
   exp($n - 1, @primes[$n - 1]) !!
   do {
       my $i = 0;
       my $iterator = $n %% 2 ?? (1..*) !! (1..*).map: *²;
       $iterator.first: {
           next unless $n == .&div-count;
           next unless ++$i == $n;
           $_
       }
   }

};</lang>

First 15 terms of OEIS:A073916
1 3 25 14 14641 44 24137569 70 1089 405 819628286980801 160 22563490300366186081 2752 9801