Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A highly composite number is a positive integer that has more divisors than any smaller positive integer has. This is OEIS sequence A002182. Its first 20 terms are

1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560

For example, 4 is in the sequence because it has 3 divisors (namely 1, 2, 4), whereas 3 only has 2 divisors, 2 also has 2 divisors, and 1 has 1 divisors.

Challenge

Given a positive integer input n, output either the n-th highly composite number or the first n highly composite numbers, at your choice (but the choice must be the same for every input n).

Rules

The program or function should theoretically work for arbitrarily large inputs given infinite time and memory, and without considering data type limitations. Essentially, this means no hardcoding a finite number of values.

In practice, the program or function should run in a reasonable amount of time, say less than 1 minute, for n up to 20. Maximum input or output may be limited by your language standard data type (but again, the algorithm should theoretically work for arbitrarily large numbers).

Any reasonable input and output format is allowed, including unary.

Code golf. Fewest bytes wins.

share|improve this question
    
This conversation has been moved to chat. – Dennis Mar 1 at 13:59
    
Can the nth-index be zero-indexed or must this be 1-indexed? – Adnan Mar 1 at 17:08
    
@AandN I hadn't thought of that, so let's say both are accepted. (I seem to recall a meta post proposing both 1-based and 0-based are allowed, but I can't find it. Anyone?) – Luis Mendo Mar 1 at 17:18

10 Answers 10

up vote 4 down vote accepted

05AB1E, 15 14 bytes

The input in zero-indexed. That means that n = 0 gives 1, n = 1 gives 2, etc. Code:

$µ>DÑgD®›i©¼}\

Explanation:

$               # Pushes 1 and input
 µ              # Counting loop, executes until the counting variable is equal to input
  >             # Increment (n + 1)
   DÑ           # Duplicate and calculate all divisors
     gD         # Get the length of the array and duplicate
       ®        # Retrieve element, standardized to zero
        ›i  }   # If greater than, do...
          ©     #   Copy value into the register
           ¼    #   Increment on the counting variable
             \  # Pop the last element in the stack

Calculates n = 19, which should give 7560 in about 10 seconds.

Try it online!

Uses CP-1252 encoding.

share|improve this answer

Jelly, 15 bytes

,®ÆDL€ṛ©0>/?µƓ#

For input n, this prints the first n highly composite numbers.

For n = 20, it takes less than two seconds on Try it online!

How it works

,®ÆDL€ṛ©0>/?µƓ#  Main link. No implicit input.

            µ    Push the chain to the left on the local link stack.
             Ɠ   Read an integer n from STDIN.
              #  Execute the chain for k = 0, 1, 2, ..., until it returned a truthy
                 value n times. Return the list of matches.

,®               Pair k with the value in the register (initially 0).
  ÆD             Compute the divisors of k and the register value.
    L€           Count both lists of divisors.
           ?     If
         >/        k has more divisors than the register value:
      ṛ©             Save k in the register and return k.
        0          Else: Return 0.

Alternate version, 13 bytes (non-competing)

While the below code worked in the latest version of Jelly that precedes this challenge, the implementation of M was very slow, and it did not comply with the time limit. This has been fixed.

ÆDL®;©MḢ’>µƓ#

Try it online!

How it works

ÆDL®;©MḢ’>µƓ#  Main link. No implicit input.

          µ    Push the chain to the left on the local link stack.
           Ɠ   Read an integer n from STDIN.
            #  Execute the chain for k = 0, 1, 2, ..., until it returned a truthy
               value n times. Return the list of matches.

ÆD             Compute the divisors of k.
  L            Count them.
   ®;          Append the count to the list in the register (initially 0 / [0]).
     ©         Save the updated list in the register.
      M        Obtain all indices the correspond to maximal elements.
       Ḣ       Retrieve the first result.
        ’>     Subtract 1 and compare with k.
               This essentially checks if the first maximal index is k + 2, where
               the "plus 2" accounts for two leading zeroes (initial value of the
               register and result for k = 0).
share|improve this answer
    
There's also RÆDL€MḢ=µƓ# (11 bytes), but it takes 44 minutes on my machine... – Dennis Mar 1 at 5:48

MATL, 26 24 bytes

~XKx`K@@:\~s<?@5MXKx]NG<

Try it online!

The current largest number of divisors found is kept in clipboard K. Highly composite numbers (HCN) are kept directly on the stack. A loop keeps testing candidates to HCN. When one is found it is left on the stack, and clipboard K is updated. The loop exits when the desired number of HCN have been found.

~         % take input implicitly (gets stored in clipboard G). Transform into a 0 
          % by means of logical negation
XKx       % copy that 0 into clipboard K, and delete
`         % do...while
  K       %   push largest number of divisors found up to now
  @       %   push iteration index, i: current candidate to HCN
  @:      %   range [1,...,i]
  \       %   modulo operation
  ~s      %   number of zeros. This is the number of divisors of current candidate
  <       %   is it larger than previous largest number of divisors?
  ?       %   if so: a new HCN has been found
    @     %     push that number
    5M    %     push the number of divisors that was found
    XKx   %     update clipboard K, and delete
  ]       %   end if
  N       %   number of elements in stack
  G<      %   is it less than input? This the loop condition: exit when false
          % end do...while implicitly
          % display all numbers implicitly
share|improve this answer

Perl, 60 57 + 1 = 58 bytes

$,++;$==grep$,%$_<1,1..$,;$_--,$m=$=if$=>$m;$_?redo:say$,

Requires -n and the free -M5.010|-E:

$ perl -nE'$,++;$==grep$,%$_<1,1..$,;$_--,$m=$=if$=>$m;$_?redo:say$,' <<< 10 
120

How it works:

$,++;
     $==grep$,%$_<1,1..$,;                                # Calculate total numbers of divisors for `$,`
                          $_--,$m=$=if$=>$m;              # Set `$m`ax divisors to `$=`ivisors if `$m>$=`
                                            $_?redo:say$, # Repeat or print
share|improve this answer

JavaScript (ES6) 72

Straightforward implementation. Time near 20 sec for input 20

x=>{for(i=e=n=0;i<x;d>e&&(++i,e=d))for(d=1,j=++n;--j;)n%j||++d;return n}

The eval trick could save a byte doubling the run time

x=>eval("for(i=e=n=0;i<x;d>e&&(++i,e=d))for(d=1,j=++n;--j;)n%j||++d;n")

Less golfed

x=>{
  for(i = e = 0, n = 1; i < x; n++)
  {
    for(d = 1, j = n; --j; )
    {
      if (n%j == 0) 
        ++d;
    }
    if (d > e)
      ++i,
      e = d;
  }
  return n;
}
share|improve this answer

Pyth, 17 16 bytes

1 byte thanks to Jakube

uf<Fml{yPd,GTGQ1

Test suite

Takes a 0-indexed n, and returns the nth highly composite number.

Explanation:

uf<Fml{yPd,GThGQ1
                     Input: Q = eval(input())
u              Q1    Apply the following function Q times, starting with 1.
                     Then output the result. lambda G.
 f           hG      Count up from G+1 until the following is truthy, lambda T.
          ,GT        Start with [G, T] (current highly comp., next number).
    m                Map over those two, lambda d.
        Pd           Take the prime factorization of d, with multiplicity.
       y             Take all subsets of those primes.
      {              Deduplicate. At this point, we have a list of lists of primes.
                     Each list is the prime factorization of a different factor.
     l               Take the length, the number of factors.
  <F                 Check whether the number of factors of the previous highly
                     composite number is smaller than that of the current number.
share|improve this answer

Ruby, 70 69 67 66 64 62

->n{r=k=0
0until(r<t=(1..k+=1).count{|d|k%d<1})&&1>n-=t/r=t
k}

Straightforward implementation.

share|improve this answer

C, 98 bytes

f,i,n,j;main(m){for(scanf("%d",&n);n--;printf("%d ",i))for(m=f;f<=m;)for(j=++i,f=0;j;)i%j--||f++;}

Try it here.

Ungolfed

f,i,n,j;

main(m)
{
    for(scanf("%d",&n); /* Get input */
            n--; /* Loop while still HCN's to calculate... */
            printf("%d ",i)) /* Print out the last calculated HCN */
        for(m=f;f<=m;) /* Loop until an HCN is found... */
            for(j=++i,f=0;j;) /* Count the number of factors */
                i%j--||f++;
}
share|improve this answer

Python 3, 97 bytes

i=p=q=0;n=int(input())
while q<n:
 c=j=0;i+=1
 while j<i:j+=1;c+=i%j==0
 if c>p:p=c;q+=1
print(i)

A full program that takes input from STDIN and prints the output to STDOUT. This returns the nth 1-indexed highly composite number.

How it works

This is a straightforward implementation. The input n is the highly composite number index.

The program iterates over the integers i. For each integer j less than i, i mod j is taken; if this is 0, j must be a factor of i and the counter c is incremented, giving the number of divisors of i after looping. p is the previous highest number of divisors, so if c > p, a new highly composite number has been found and the counter q is incremented. Once q = n, i must be the nth highly composite number, and this is printed.

Try it on Ideone

(This takes ~15 seconds for n = 20, which exceeds the time limit for Ideone. Hence, the example given is for n = 18.)

share|improve this answer

Python 2, 207 bytes

n,i,r,o=input(),1,[],[]
while len(o)<n:
 r+=[(lambda n:len(set(reduce(list.__add__,([i,n//i]for i in range(1,int(n**0.5)+1)if n%i==0)))))(i)];h=max(r)
 if r.index(h)>i-2 and r.count(h)<2:o+=[i]
 i+=1
print o

Uses the same method as Dennis' Jelly answer. Calculates the first 20 terms in <2 seconds.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.