How to find the smallest number in the the range 1 to 100 that has the most divisors? I know a trivial way would be to check the divisors of each number from 1 upto 100 and keep track of the number having maximum divisor. But is there an more efficient way?
-
2This could help http://stackoverflow.com/questions/110344/algorithm-to-calculate-the-number-of-divisors-of-a-given-number – GoofyHTS Dec 04 '12 at 17:50
-
3for 100 your approach is just fine, `O(n^2)` for such a small input should not be a big issue. – amit Dec 04 '12 at 17:50
-
what if the range is very large say into millions? – jairaj Dec 04 '12 at 17:52
-
2Then it is a different problem that should be handled differently. Is it the case? If so, please edit your question to indicate it. – amit Dec 04 '12 at 17:52
-
Then look at the link that @GoofyHTS provided. – Xymostech Dec 04 '12 at 17:53
-
yes.I want to know the efficient way to do so. – jairaj Dec 04 '12 at 17:54
-
does divisor mean disticnt divisors ?? for eg no .of divisors of 4 is 2 or 1 ?? – Wayne Rooney Dec 04 '12 at 17:54
-
2start with 2 and keep multiplying primes till you hit 100. – ashley Dec 04 '12 at 17:56
-
it means that i need to calculate the number of divisors of each number.The only part i can improvise is the calculation of number of divisors. right ?? – jairaj Dec 04 '12 at 17:58
-
one way would be to avoid odd numbers.. – Anirudha Dec 04 '12 at 18:05
5 Answers
For smallish bounds, using a sieve would be my good enough. From the fact
r r
(1) n = ∏ p_k^e_k => τ(n) = ∏ (e_k + 1)
k=1 k=1
it is clear that the number of divisors can easily be determined from the prime factorisation of n
, and that τ(m*n) = τ(m) * τ(n)
if gcd(m,n) = 1
(i.e. τ is a multiplicative function).
So we can cheaply compute τ(n)
if we know any prime factor of n
and all τ(m)
for 1 <= m < n
. Thus
int sieve[limit+1];
// initialise sieve
for(int i = 0; i <= limit; ++i) {
sieve[i] = i;
}
// find a prime factor for all numbers > 1
int root = sqrt(limit); // limit is supposed to be not too large, so no fixup needed here
for(int p = 2; p <= root; ++p) {
if (sieve[p] == p) {
// this means p is prime, mark multiples
for(int m = p*p; m <= limit; m += p) {
sieve[m] = p;
}
}
// Now sieve[n] is a prime factor of n
int p;
for(int n = 2; n <= limit; ++n) {
if ((p = sieve[n]) == n) {
// a prime, two divisors
sieve[n] = 2;
} else {
// count the multiplicity of p in n and find the cofactor of p^multiplicity
int m = 1, q = n;
do {
q /= p;
++m;
}while(q % p == 0);
sieve[n] = m*sieve[q];
}
}
// Now sieve[n] contains τ(n), the number of divisors of n, look for the maximum
int max_div = 0, max_num = 0;
for(int n = 1; n <= limit; ++n) {
if (sieve[n] > max_div) {
max_div = sieve[n];
max_num = n;
}
}
finds the smallest number with the largest divisor count not exceeding N
in O(N*log log N)
time, with a relatively small constant factor (that could be reduced further by treating 2 separately and only marking odd multiples of odd primes).
That is a simple brute-force method that is fast enough for small N
(the interpretation of "small" depends on the notion of "fast enough", could be <= 1000
or <= 1000000
for example).
For larger bounds, that is too slow and too memory intensive. For those, we need to do a bit more analysis.
From (1), we can deduce that among all numbers with the same structure of the prime factorisation (meaning the same number r
of distinct prime factors, and the same multiset of exponents, but possibly in different order), that all have the same number of divisors, the smallest is the one where
- the prime factors are the
r
smallest primes - the exponents appear in descending order (2 has the largest exponent, 3 the next largest, ...)
So we can find the smallest number with the most divisors <= N
by considering all finite sequences
e_1 >= e_2 >= ... >= e_r > 0
with the property
r
N/2 < n(e_1, ..., e_r) = ∏ p_k^e_k <= N
k=1
and the sought number is one of the n(e_1, ..., e_r)
produced by them. (If n(e_i) <= N/2
for a monotonic non-increasing finite sequence, the sequence with 1 added to e_1
would produce a number <= N
with more divisors.)
The largest divisor count is produced for exponents that are roughly proportional to 1/log p_k
. More precisely, for a fixed r
, let
r
T(x_1, ..., x_r) = ∏ (x_k+1)
k=1
r
F(x_1, ..., x_r) = ∏ p_k^x_k
k=1
Then T
assumes its maximal value on the set { x : F(x) = N and x_k > 0 for all k }
in the point with
r
x_k = (log N + ∑ log p_k)/(r * log p_k) - 1
k=1
We only admit integer exponents, which complicates the matter, but straying too far from the proportionality produces numbers with fewer divisors than we find near the proportionality.
Let's illustrate it for N = 100000
(it's a bit too small to really exploit the proportionality, but small enough to completely do by hand):
r = 1
:e_1 = 16
,n(16) = 2^16 = 65536
has 17 divisors.r = 2
: Settingx_2 = x_1 * log 2 / log 3
andN = 2^x_1 * 3^x_2 = 2^(2*x_1)
, we obtainx_1 ≈ 8.3, x_2 ≈ 5.24
. Now let's see what happens withe_1, e_2
close tox_1, x_2
.2^7 *3^6 = 93312, τ(2^7 *3^6) = (7+1)*(6+1) = 56 2^8 *3^5 = 62208, τ(2^8 *3^5) = (8+1)*(5+1) = 54 2^10*3^4 = 82944, τ(2^10*3^4) = (10+1)*(4+1) = 55
straying farther away from the proportionality reduces the divisor count quickly,
2^11*3^3 = 55296, τ(2^11*3^3) = (11+1)*(3+1) = 48 2^13*3^2 = 73728, τ(2^13*3^2) = (13+1)*(2+1) = 42 2^15*3^1 = 98304, τ(2^15*3^1) = (15+1)*(1+1) = 32
So the closest pair to the proportionality didn't produce the largest divisor count, but the ones with the large divisor counts were the closest three.
r = 3
: Similarly, we obtainx_1 ≈ 5.5, x_2 ≈ 3.5, x_3 ≈ 2.4
2^4 *3^3*5^3 = 54000, τ(2^4 *3^3*5^3) = 5*4*4 = 80 2^5 *3^4*5^2 = 64800, τ(2^5 *3^4*5^2) = 6*5*3 = 90 2^7 *3^3*5^2 = 86400, τ(2^7 *3^3*5^2) = 8*4*3 = 96 2^8 *3^2*5^2 = 57600, τ(2^8 *3^2*5^2) = 9*3*3 = 81 2^6 *3^5*5^1 = 77760, τ(2^6 *3^5*5^1) = 7*6*2 = 84 2^7 *3^4*5^1 = 51840, τ(2^7 *3^4*5^1) = 8*5*2 = 80 2^9 *3^3*5^1 = 69120, τ(2^9 *3^3*5^1) = 10*4*2 = 80 2^11*3^2*5^1 = 92160, τ(2^11*3^2*5^1) = 12*3*2 = 72 2^12*3^1*5^1 = 61440, τ(2^12*3^1*5^1) = 13*2*2 = 52
again, the large divisor counts are achieved for exponents close to the proportionality.
r = 4
: The rough approximations to the exponents arex_1 ≈ 4.15, x_2 ≈ 2.42, x_3 ≈ 1.79, x_4 ≈ 1.48
. Fore_4 = 2
, there is only one choice,2^3*3^2*5^2*7^2 = 88200, τ(2^3*3^2*5^2*7^2) = 4*3*3*3 = 108
For
e_4 = 1
, we have a few more choices:2^4*3^3*5^2*7^1 = 75600, τ(2^4*3^3*5^2*7^1) = 5*4*3*2 = 120 2^5*3^2*5^2*7^1 = 50400, τ(2^5*3^2*5^2*7^1) = 6*3*3*2 = 108 2^5*3^4*5^1*7^1 = 90720, τ(2^5*3^4*5^1*7^1) = 6*5*2*2 = 120 2^6*3^3*5^1*7^1 = 60480, τ(2^6*3^3*5^1*7^1) = 7*4*2*2 = 112 2^8*3^2*5^1*7^1 = 80640, τ(2^8*3^2*5^1*7^1) = 9*3*2*2 = 108 2^9*3^1*5^1*7^1 = 53760, τ(2^9*3^1*5^1*7^1) = 10*2*2*2 = 80
r = 5
:x_1 ≈ 3.3, x_2 ≈ 2.1, x_3 ≈ 1.43, x_4 ≈ 1.18, x_5 ≈ 0.96
. Since2*3*5*7*11 = 2310
, the exponents of 7 and 11 must be 1, we find the candidates2^2*3^2*5^2*7*11 = 69300, τ(2^2*3^2*5^2*7*11) = 3*3*3*2*2 = 108 2^3*3^3*5^1*7*11 = 83160, τ(2^3*3^3*5^1*7*11) = 4*4*2*2*2 = 128 2^4*3^2*5^1*7*11 = 55440, τ(2^4*3^2*5^1*7*11) = 5*3*2*2*2 = 120 2^6*3^1*5^1*7*11 = 73920, τ(2^6*3^1*5^1*7*11) = 7*2*2*2*2 = 112
r = 6
: Since2*3*5*7*11*13 = 30030
, there is only one candidate here,2^2*3*5*7*11*13 = 60060, τ(60060) = 3*2^5 = 96
and that produces a smaller divisor count than the best candidates using four or five primes.
So we investigated 28 candidates (and could have skipped several of them) to find that the smallest number <= 100000
with the most divisors is 83160 (98280 is the other number below 100000 with 128 divisors).
Here's a programme that finds the smallest number with the most divisors not exceeding a given limit < 2^64
practically instantaneously (no attempts at short-cutting have been made since it's fast enough as is for 64-bit integers, for arbitrary precision integers, that would become worthwhile at some point):
#include <stdlib.h>
#include <stdio.h>
typedef struct {
unsigned long long number;
unsigned long long divisors;
} small_max;
static const unsigned long long primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 };
static const unsigned long long primorials[] =
{ 2, 6, 30, 210, 2310, 30030, 510510, 9699690, 223092870, 6469693230,
200560490130, 7420738134810, 304250263527210, 13082761331670030,
614889782588491410 };
static const unsigned num_primes = sizeof primorials / sizeof primorials[0];
small_max max_divisors(unsigned long long limit);
small_max best_with(unsigned long long limit, unsigned index, unsigned multiplicity);
void factor(unsigned long long number);
int main(int argc, char *argv[]) {
unsigned long long limit;
limit = argc > 1 ? strtoull(argv[1],NULL,0) : 100000;
small_max best = max_divisors(limit);
printf("\nSmallest number not exceeding %llu with most divisors:\n",limit);
printf("%llu with %llu divisors\n", best.number, best.divisors);
factor(best.number);
return 0;
}
small_max max_divisors(unsigned long long limit) {
small_max result;
if (limit < 3) {
result.number = limit;
result.divisors = limit;
return result;
}
unsigned idx = num_primes;
small_max best = best_with(limit,0,1);
printf("Largest power of 2: %llu = 2^%llu\n", best.number, best.divisors-1);
for(idx = 1; idx < num_primes && primorials[idx] <= limit; ++idx) {
printf("Using primes to %llu:\n", primes[idx]);
unsigned long long test = limit, remaining = limit;
unsigned multiplicity = 0;
do {
++multiplicity;
test /= primorials[idx];
remaining /= primes[idx];
result = best_with(remaining, idx-1, multiplicity);
for(unsigned i = 0; i < multiplicity; ++i) {
result.number *= primes[idx];
}
result.divisors *= multiplicity + 1;
if (result.divisors > best.divisors) {
printf("New largest divisor count: %llu for\n ", result.divisors);
factor(result.number);
best = result;
} else if (result.divisors == best.divisors && result.number < best.number) {
printf("Smaller number with %llu divisors:\n ", result.divisors);
factor(result.number);
best = result;
}
}while(test >= primorials[idx]);
}
return best;
}
small_max best_with(unsigned long long limit, unsigned index, unsigned multiplicity) {
small_max result = {1, 1};
if (index == 0) {
while(limit > 1) {
result.number *= 2;
++result.divisors;
limit /= 2;
}
return result;
}
small_max best = {0,0};
unsigned long long test = limit, remaining = limit;
--multiplicity;
for(unsigned i = 0; i < multiplicity; ++i) {
test /= primorials[index];
remaining /= primes[index];
}
do {
++multiplicity;
test /= primorials[index];
remaining /= primes[index];
result = best_with(remaining, index-1, multiplicity);
for(unsigned i = 0; i < multiplicity; ++i) {
result.number *= primes[index];
}
result.divisors *= multiplicity + 1;
if (result.divisors > best.divisors) {
best = result;
} else if (result.divisors == best.divisors && result.number < best.number) {
best = result;
}
}while(test >= primorials[index]);
return best;
}
void factor(unsigned long long number) {
unsigned long long num = number;
unsigned idx, mult;
printf("%llu =", number);
for(idx = 0; num > 1 && idx < num_primes; ++idx) {
mult = 0;
while(num % primes[idx] == 0) {
num /= primes[idx];
++mult;
}
printf("%s %llu ^ %u", idx ? " *" : "", primes[idx], mult);
}
printf("\n");
}

- 181,706
- 17
- 308
- 431
For each number from 1 to 100 you can check all of it's multiples and add the number of divisors. Depending on how you are checking the divisors of each number, it can be more efficient. Here is a python code that does this idea. The complexity is O(N log N)
count=[0]*101
for i in xrange(1,101):
for j in xrange(1,100/i+1):
count[i*j]+=1
print max(zip(count,xrange(101)))
And here is the code in C
int i,j,count[101];
for(i=1;i<=100;i++) for(j=1;j<=100/i;j++) count[i*j]++;
int max=-1,pos;
for(i=1;i<=100;i++) if(count[i]>=max){
max=count[i];
pos=i;
}
printf("%d has %d divisors\n",pos,max);
Both versions keep the maximum number out of all the numbers with maximum divisors. In this case 96 has 12 divisors.

- 548
- 2
- 10
-
I can't add comments to other posted answers, but wanted to mention this solution calculates the number of divisors while the others count the number of prime factors. Although they are related, I don't see how they calculate the number of divisors without knowing the power of each prime factor (and you need this to calculate the number of divisors.) – bcurcio Dec 04 '12 at 18:22
-
The C code's output would match Python's if you checked `if (count[i] >= max)`. I'd prefer to get all of the numbers with the maximal number of divisors, but that is of course a bit more complicated. And unless you attract a few downvotes, you will have the [comment](http://stackoverflow.com/privileges/comment) privilege in the future. – Daniel Fischer Dec 04 '12 at 18:49
-
are you sure the complexity is nlogn because i don't see it.it is actually of n^2 – jairaj Dec 04 '12 at 19:08
-
1Yes, complexity is O(N log N). Think of it how many times you go into the inner cycle. When i=1 you do N steps of j, when i=2 you do N/2 steps of j, with i=3 you do N/3. From i=j/2 to j you do only 1 step of j. So you have the sum from 1 to N of floor(N/i). This is known to be O(N log N), you can code a simple checker if you are not convinced. – bcurcio Dec 04 '12 at 19:15
-
@bcurcio It would perhaps be more obvious if you wrote `xrange(1, 100//i + 1)` resp `for(j = 1; j <= 100/i; ++j)`. – Daniel Fischer Dec 04 '12 at 19:20
-
@DanielFischer Ok, I accept those suggestions. I thought it might be more intuitive the other way, but it's true that this way is easier to see the time complexity this way. – bcurcio Dec 04 '12 at 19:27
There is an "easier way", but it's theoretical, not really a computer algorithm. Two different cases arise - one if by "most factors" you mean just that, and the other if the factors have to be unique.
In the first case, you just need to recognize that, to maximize the number of factors, each factor needs to be as small as possible, i.e. 2. The number less than 100 that has the most factors, is thus the largest power of 2 less than 100, which happens to be 64.
If the factors must be unique, then we simply use 2, 3, 5, etc. (the prime numbers) until the next cumulative product is greater than 100 - in this case 2*3*5=30 is the number that has the most unique factors. Adding a fourth factor would make it 210, so that's as high as we can go.

- 59,951
- 11
- 89
- 84
-
`std::uint8_t get_integer_with_most_divisors_between_1_and_100() { return 42; }`. Ah wait, that should be `64` (or `30` if uniqueness if required). – rubenvb Dec 04 '12 at 18:21
-
10This is wrong. `64 = 2^6` has 7 divisors, but `60 = 2^2 * 3 * 5`, `72 = 2^3 * 3^2` and `96 = 2^5 * 3` all have 12 divisors. – Daniel Fischer Dec 04 '12 at 18:38
-
-
-
@DanielFischer Good point, if the OP actually meant divisors (which admittedly was the word originally used). Most of the other answers/comments seemed to be interpreting the question as asking about factors, so I made the same assumption, but actually used the word factor. If divisors were actually meant, it's a significantly different (and harder) problem. – twalberg Dec 04 '12 at 18:52
-
@jairaj Yes, you would need to know the prime numbers, which is a well known problem with several different algorithms depending on the range you're talking about. For the stated range of 100, though, pretty much anyone who's had any math education at all should be able to name the first few primes... – twalberg Dec 04 '12 at 18:54
-
@jairaj : See my solution (http://stackoverflow.com/a/13709224/1461963) which does both the thing . – Wayne Rooney Dec 04 '12 at 19:27
You can take some idea from the sieve of Eratosthenes algorithm .Only thing is that you need to run the inner loop from 2*i rather than i*i. But this algorithm is faster than O(n^2)
int a[]=new int[101],max=0,index=-1;
for(i=2;i<=100;i++)
{
if(a[i]==0)
for(j=2*i;j<=100;j+=i)
a[j]++;
if(a[i]>max)
{
index=i;
max=a[i];
}
This gives you 30 with number of divisor as 3. You can modify the inner loop if you want variants in the answer

- 1,587
- 3
- 17
- 23
one way would be to avoid odd numbers..
int mostDivisors(int min,int max)
{
int i,j,pc=0,cc=0,no=0;
min=(min%2==0)?min:min+1;//making it even
for(i=min;i<=max;i+=2)//checking only even numbers
{
cc=0;
for(j=2;j<i;j++)//avoiding dividing by 1 and itself
{
if(i%j==0)cc++;
}
if(pc<cc)
{
no=i;
pc=cc;
}
}
return no;
}

- 32,393
- 7
- 68
- 89