To ensure that a number not exceeding has the maximum number of divisors, it should be represented as the product of the smallest possible prime numbers.
For example, consider numbers of the form 2^k. Find the largest such that 2^k \le n. The number of divisors of 2^k is given by d(2^k) = k + 1.
Next, examine the number 2^{k - 1} \cdot 3. The number of its divisors is d(2^{k - 1} \cdot 3) = k \cdot 2, which is greater than k + 1.
Thus:
If 2^{k - 1} \cdot 3 \le n, the desired number is 2^{k - 1} \cdot 3;
Otherwise, the answer is 2^k.
Finally, consider the special case when n = 1. For this value, the answer is .
Example
Let n = 100. The largest power of not exceeding is 2^6 = 64. The number has d(64) = 6 + 1 = 7 divisors.
Now, consider the number 2^5 \cdot 3 = 96. The number of its divisors is:
Since does not exceed , it will be the desired number for n = 100.
Algorithm implementation
Read the input value of .
scanf("%d",&n);Find the largest p = 2^k such that p \le n.
p = 1;
while (p * 2 <= n)
p = p * 2;Next, compute p1 = 2^{k - 1} \cdot 3 = p / 2 \cdot 3.
p1 = p / 2 * 3;Determine the result . If n = 1, the result will be .
if (p1 <= n) res = p1; else res = p;
if (n == 1) res = 1;Print the answer.
printf("%d\n",res);Python implementation
Read the input value of .
n = int(input())Find the largest p = 2^k such that p \le n.
p = 1
while p * 2 <= n:
p *= 2Next, compute p1 = 2^{k - 1} \cdot 3 = p / 2 \cdot 3.
p1 = (p // 2) * 3Determine the result . If n = 1, the result will be .
if p1 <= n: res = p1
else: res = p
if n == 1: res = 1Print the answer.
print(res)