Sequence: smallest number with exactly n divisors

From Rosetta Code
Revision as of 16:56, 11 April 2019 by rosettacode>Gerard Schildberger (→‎{{header|REXX}}: added the REXX computer programming language for this task.)
Sequence: smallest 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 smallest natural number that has exactly n divisors.

Task

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

See also
Related tasks

Go

<lang go>package main

import "fmt"

func countDivisors(n int) int {

   count := 0
   for i := 1; i*i <= n; i++ {
       if n%i == 0 {
           if i == n/i {
               count++
           } else {
               count += 2
           }
       }
   }
   return count

}

func main() {

   const max = 15
   seq := make([]int, max)
   fmt.Println("The first", max, "terms of the sequence are:")
   for i, n := 1, 0; n < max; i++ {
       if k := countDivisors(i); k <= max && seq[k-1] == 0 {
           seq[k-1] = i
           n++
       }
   }
   fmt.Println(seq)

}</lang>

Output:
The first 15 terms of the sequence are:
[1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144]

Perl

Library: ntheory

<lang perl>use strict; use warnings; use ntheory 'divisors';

print "First 15 terms of OEIS: A005179\n"; for my $n (1..15) {

   my $l = 0;
   while (++$l) {
       print "$l " and last if $n == divisors($l);
   }

}</lang>

Output:
First 15 terms of OEIS: A005179
1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144

REXX

<lang rexx></lang>

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;

put "First $limit terms of OEIS:A005179"; put (1..$limit).map: -> $n { first { $n == .&div-count }, 1..Inf };

</lang>

Output:
First 15 terms of OEIS:A005179
1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144