Querying maximum number of divisors that a number in a given range has | JavaScript implementation of the above idea to process queries of finding a number with maximum divisors . ; 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 ; Initialize the smallest_prime factors of all to infinity ; to be built like eratosthenes sieve ; prime number will have its smallest_prime equal to itself ; if ' i ' is the first prime number reaching ' j ' ; 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 ; 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 ; use p ^ k , initialize k to 0 ; builds segment tree for divisors [ ] array ; leaf node ; build left and right subtree ; combine the information from left and right subtree at current node ; returns the maximum number of divisors in [ l , r ] ; If current node 's range is disjoint  with query range ; If the current node stores information for the range that is completely inside the query range ; Returns maximum number of divisors from left or right subtree ; First find smallest prime divisors for all the numbers ; Then build the divisors [ ] array to store the number of divisors ; Build segment tree for the divisors [ ] array
let maxn = 10005 ; let INF = 999999 ; let smallest_prime = new Array ( maxn ) ; for ( let i = 0 ; i < maxn ; i ++ ) { smallest_prime [ i ] = 0 ; } let divisors = new Array ( maxn ) ; for ( let i = 0 ; i < maxn ; i ++ ) { divisors [ i ] = 0 ; } let segmentTree = new Array ( 4 * maxn ) ; for ( let i = 0 ; i < 4 * maxn ; i ++ ) { segmentTree [ i ] = 0 ; } function findSmallestPrimeFactors ( ) { for ( let i = 0 ; i < maxn ; i ++ ) smallest_prime [ i ] = INF ; for ( let i = 2 ; i < maxn ; i ++ ) { if ( smallest_prime [ i ] == INF ) { smallest_prime [ i ] = i ; for ( let j = i * i ; j < maxn ; j += i ) { if ( smallest_prime [ j ] > i ) smallest_prime [ j ] = i ; } } } } function buildDivisorsArray ( ) { for ( let i = 1 ; i < maxn ; i ++ ) { divisors [ i ] = 1 ; let n = i ; let p = smallest_prime [ i ] let k = 0 ; while ( n > 1 ) { n = Math . floor ( n / p ) ; k ++ ; if ( smallest_prime [ n ] != p ) { divisors [ i ] = divisors [ i ] * ( k + 1 ) ; k = 0 ; } p = smallest_prime [ n ] ; } } } function buildSegtmentTree ( node , a , b ) { if ( a == b ) { segmentTree [ node ] = divisors [ a ] ; return ; } buildSegtmentTree ( 2 * node , a , Math . floor ( ( a + b ) / 2 ) ) ; buildSegtmentTree ( ( 2 * node ) + 1 , Math . floor ( ( a + b ) / 2 ) + 1 , b ) ; segmentTree [ node ] = Math . max ( segmentTree [ 2 * node ] , segmentTree [ ( 2 * node ) + 1 ] ) ; } function query ( node , a , b , l , r ) { if ( l > b a > r ) return - 1 ; if ( a >= l && b <= r ) return segmentTree [ node ] ; return Math . max ( query ( 2 * node , a , Math . floor ( ( a + b ) / 2 ) , l , r ) , query ( 2 * node + 1 , Math . floor ( ( a + b ) / 2 ) + 1 , b , l , r ) ) ; } findSmallestPrimeFactors ( ) ; buildDivisorsArray ( ) ; buildSegtmentTree ( 1 , 1 , maxn - 1 ) ; document . write ( " " + " " + query ( 1 , 1 , maxn - 1 , 1 , 100 ) + " " ) ; document . write ( " " + " " + query ( 1 , 1 , maxn - 1 , 10 , 48 ) + " " ) ; document . write ( " " + " " + query ( 1 , 1 , maxn - 1 , 1 , 10 ) + " " ) ;