# Querying maximum number of divisors that a number in a given range has

Given Q queries, of type: **L R**, for each query you must print the maximum number of divisors that a number **x (L <= x <= R)** has.

**Examples:**

L = 1 R = 10: 1 has 1 divisor. 2 has 2 divisors. 3 has 2 divisors. 4 has 3 divisors. 5 has 2 divisors. 6 has 4 divisors. 7 has 2 divisors. 8 has 4 divisors. 9 has 3 divisors. 10 has 4 divisors. So the answer for above query is 4, as it is the maximum number of divisors a number has in [1, 10].

**Pre-requisites : **Eratosthenes Sieve, Segment Tree

Below are steps to solve the problem.

- Firstly, let’s see how many number of divisors does a number
**n = p**(where p_{1}^{k1}* p_{2}^{k2}* … * p_{n}^{kn}_{1}, p_{2}, …, p_{n}are prime numbers) has; the answer is**(k**. How? For each prime number in the prime factorization, we can have its_{1}+ 1)*(k_{2}+ 1)*…*(k_{n}+ 1)**k**possible powers in a divisor (0, 1, 2,…, k_{i}+ 1_{i}). - Now let’s see how can we find the prime factorization of a number, we firstly build an array,
**smallest_prime[]**, which stores the smallest prime divisor of**i**at**i**index, we divide a number by its smallest prime divisor to obtain a new number (we also have the smallest prime divisor of this new number stored), we keep doing it until the smallest prime of the number changes, when the smallest prime factor of the new number is different from the previous number’s, we have k^{th}_{i}for the i^{th}prime number in the prime factorization of the given number. - Finally, we obtain the number of divisors for all the numbers and store these in a segment tree that maintains the maximum numbers in the segments. We respond to each query by querying the segment tree.

## C++

`// A C++ implementation of the above idea to process ` `// queries of finding a number with maximum divisors. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define maxn 1000005 ` `#define INF 99999999 ` ` ` `int` `smallest_prime[maxn]; ` `int` `divisors[maxn]; ` `int` `segmentTree[4 * maxn]; ` ` ` `// Finds smallest prime factor of all numbers in ` `// range[1, maxn) and stores them in smallest_prime[], ` `// smallest_prime[i] should contain the smallest prime ` `// that divides i ` `void` `findSmallestPrimeFactors() ` `{ ` ` ` `// Initialize the smallest_prime factors of all ` ` ` `// to infinity ` ` ` `for` `(` `int` `i = 0 ; i < maxn ; i ++ ) ` ` ` `smallest_prime[i] = INF; ` ` ` ` ` `// to be built like eratosthenes sieve ` ` ` `for` `(` `long` `long` `i = 2; i < maxn; i++) ` ` ` `{ ` ` ` `if` `(smallest_prime[i] == INF) ` ` ` `{ ` ` ` `// prime number will have its smallest_prime ` ` ` `// equal to itself ` ` ` `smallest_prime[i] = i; ` ` ` `for` `(` `long` `long` `j = i * i; j < maxn; j += i) ` ` ` ` ` `// if 'i' is the first prime number reaching 'j' ` ` ` `if` `(smallest_prime[j] > i) ` ` ` `smallest_prime[j] = i; ` ` ` `} ` ` ` `} ` `} ` ` ` `// number of divisors of n = (p1 ^ k1) * (p2 ^ k2) ... (pn ^ kn) ` `// are equal to (k1+1) * (k2+1) ... (kn+1) ` `// this function finds the number of divisors of all numbers ` `// in range [1, maxn) and stores it in divisors[] ` `// divisors[i] stores the number of divisors i has ` `void` `buildDivisorsArray() ` `{ ` ` ` `for` `(` `int` `i = 1; i < maxn; i++) ` ` ` `{ ` ` ` `divisors[i] = 1; ` ` ` `int` `n = i, p = smallest_prime[i], k = 0; ` ` ` ` ` `// we can obtain the prime factorization of the number n ` ` ` `// n = (p1 ^ k1) * (p2 ^ k2) ... (pn ^ kn) using the ` ` ` `// smallest_prime[] array, we keep dividing n by its ` ` ` `// smallest_prime until it becomes 1, whilst we check ` ` ` `// if we have need to set k zero ` ` ` `while` `(n > 1) ` ` ` `{ ` ` ` `n = n / p; ` ` ` `k ++; ` ` ` ` ` `if` `(smallest_prime[n] != p) ` ` ` `{ ` ` ` `//use p^k, initialize k to 0 ` ` ` `divisors[i] = divisors[i] * (k + 1); ` ` ` `k = 0; ` ` ` `} ` ` ` ` ` `p = smallest_prime[n]; ` ` ` `} ` ` ` `} ` `} ` ` ` `// builds segment tree for divisors[] array ` `void` `buildSegtmentTree(` `int` `node, ` `int` `a, ` `int` `b) ` `{ ` ` ` `// leaf node ` ` ` `if` `(a == b) ` ` ` `{ ` ` ` `segmentTree[node] = divisors[a]; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `//build left and right subtree ` ` ` `buildSegtmentTree(2 * node, a, (a + b) / 2); ` ` ` `buildSegtmentTree(2 * node + 1, ((a + b) / 2) + 1, b); ` ` ` ` ` `//combine the information from left ` ` ` `//and right subtree at current node ` ` ` `segmentTree[node] = max(segmentTree[2 * node], ` ` ` `segmentTree[2 *node + 1]); ` `} ` ` ` `//returns the maximum number of divisors in [l, r] ` `int` `query(` `int` `node, ` `int` `a, ` `int` `b, ` `int` `l, ` `int` `r) ` `{ ` ` ` `// If current node's range is disjoint with query range ` ` ` `if` `(l > b || a > r) ` ` ` `return` `-1; ` ` ` ` ` `// If the current node stores information for the range ` ` ` `// that is completely inside the query range ` ` ` `if` `(a >= l && b <= r) ` ` ` `return` `segmentTree[node]; ` ` ` ` ` `// Returns maximum number of divisors from left ` ` ` `// or right subtree ` ` ` `return` `max(query(2 * node, a, (a + b) / 2, l, r), ` ` ` `query(2 * node + 1, ((a + b) / 2) + 1, b,l,r)); ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `// First find smallest prime divisors for all ` ` ` `// the numbers ` ` ` `findSmallestPrimeFactors(); ` ` ` ` ` `// Then build the divisors[] array to store ` ` ` `// the number of divisors ` ` ` `buildDivisorsArray(); ` ` ` ` ` `// Build segment tree for the divisors[] array ` ` ` `buildSegtmentTree(1, 1, maxn - 1); ` ` ` ` ` `cout << ` `"Maximum divisors that a number has "` ` ` `<< ` `" in [1, 100] are "` ` ` `<< query(1, 1, maxn - 1, 1, 100) << endl; ` ` ` ` ` ` ` `cout << ` `"Maximum divisors that a number has"` ` ` `<< ` `" in [10, 48] are "` ` ` `<< query(1, 1, maxn - 1, 10, 48) << endl; ` ` ` ` ` ` ` `cout << ` `"Maximum divisors that a number has"` ` ` `<< ` `" in [1, 10] are "` ` ` `<< query(1, 1, maxn - 1, 1, 10) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python 3

# Python 3 implementation of the above

# idea to process queries of finding a

# number with maximum divisors.

maxn = 1000005

INF = 99999999

smallest_prime = [0] * maxn

divisors = [0] * maxn

segmentTree = [0] * (4 * maxn)

# Finds smallest prime factor of all

# numbers in range[1, maxn) and stores

# them in smallest_prime[], smallest_prime[i]

# should contain the smallest prime that divides i

def findSmallestPrimeFactors():

# Initialize the smallest_prime

# factors of all to infinity

for i in range(maxn ):

smallest_prime[i] = INF

# to be built like eratosthenes sieve

for i in range(2, maxn):

if (smallest_prime[i] == INF):

# prime number will have its

# smallest_prime equal to itself

smallest_prime[i] = i

for j in range(i * i, maxn , i):

# if ‘i’ is the first prime

# number reaching ‘j’

if (smallest_prime[j] > i):

smallest_prime[j] = i

# number of divisors of n = (p1 ^ k1) *

# (p2 ^ k2) … (pn ^ kn) are equal to

# (k1+1) * (k2+1) … (kn+1). This function

# finds the number of divisors of all numbers

# in range [1, maxn) and stores it in divisors[]

# divisors[i] stores the number of divisors i has

def buildDivisorsArray():

for i in range(1, maxn):

divisors[i] = 1

n = i

p = smallest_prime[i]

k = 0

# we can obtain the prime factorization

# of the number n n = (p1 ^ k1) * (p2 ^ k2)

# … (pn ^ kn) using the smallest_prime[]

# array, we keep dividing n by its

# smallest_prime until it becomes 1, whilst

# we check if we have need to set k zero

while (n > 1):

n = n // p

k += 1

if (smallest_prime[n] != p):

# use p^k, initialize k to 0

divisors[i] = divisors[i] * (k + 1)

k = 0

p = smallest_prime[n]

# builds segment tree for divisors[] array

def buildSegtmentTree( node, a, b):

# leaf node

if (a == b):

segmentTree[node] = divisors[a]

return

#build left and right subtree

buildSegtmentTree(2 * node, a, (a + b) // 2)

buildSegtmentTree(2 * node + 1,

((a + b) // 2) + 1, b)

#combine the information from left

#and right subtree at current node

segmentTree[node] = max(segmentTree[2 * node],

segmentTree[2 * node + 1])

# returns the maximum number of

# divisors in [l, r]

def query(node, a, b, l, r):

# If current node’s range is disjoint

# with query range

if (l > b or a > r):

return -1

# If the current node stores information

# for the range that is completely inside

# the query range

if (a >= l and b <= r):
return segmentTree[node]
# Returns maximum number of divisors
# from left or right subtree
return max(query(2 * node, a, (a + b) // 2, l, r),
query(2 * node + 1,
((a + b) // 2) + 1, b, l, r))
# Driver code
if __name__ == "__main__":
# First find smallest prime divisors
# for all the numbers
findSmallestPrimeFactors()
# Then build the divisors[] array to
# store the number of divisors
buildDivisorsArray()
# Build segment tree for the divisors[] array
buildSegtmentTree(1, 1, maxn - 1)
print("Maximum divisors that a number has ",
" in [1, 100] are ",
query(1, 1, maxn - 1, 1, 100))
print("Maximum divisors that a number has",
" in [10, 48] are ",
query(1, 1, maxn - 1, 10, 48))
print( "Maximum divisors that a number has",
" in [1, 10] are ",
query(1, 1, maxn - 1, 1, 10))
# This code is contributed by ita_c
[tabbyending]
**Output:**

Maximum divisors that a number has in [1, 100] are 12 Maximum divisors that a number has in [10, 48] are 10 Maximum divisors that a number has in [1, 10] are 4

**Time Complexity:**

For sieve:O(maxn * log(log(maxn)) )For calculating divisors of each number:O(k<_{1}+ k_{2}+ ... + k_{n})O(log(maxn))For querying each range:O(log(maxn))Total:O((maxn + Q) * log(maxn))

This article is contributed by **Saumye Malhotra**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Count elements in the given range which have maximum number of divisors
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Find the number of divisors of all numbers in the range [1, n]
- Program to find count of numbers having odd number of divisors in given range
- Number which has the maximum number of distinct prime factors in the range M to N
- Break a number such that sum of maximum divisors of all parts is minimum
- Find the number in a range having maximum product of the digits
- Find sum of divisors of all the divisors of a natural number
- Find the largest good number in the divisors of given number N
- Check if a number is divisible by all prime divisors of another number
- First triangular number whose number of divisors exceeds N
- Total number of divisors for a given number
- Number of divisors of a given number N which are divisible by K
- Find the sum of the number of divisors
- Sum of all the prime divisors of a number