Sum of all Perfect Cubes lying in the range [ L , R ] for Q queries | Array to precompute the sum of cubes from 1 to 100010 so that for every query , the answer can be returned in O ( 1 ) . ; Function to check if a number is a perfect Cube or not ; Find floating point value of cube root of x . ; If cube root of x is cr return the x , else 0 ; Function to precompute the perfect Cubes upto 100000. ; Function to print the sum for each query ; To calculate the precompute function ; Calling the printSum function for every query
var pref = Array ( 100010 ) . fill ( 0 ) ; function isPerfectCube ( x ) { var cr = Math . round ( Math . cbrt ( x ) ) ; return ( cr * cr * cr == x ) ? x : 0 ; } function compute ( ) { for ( var i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isPerfectCube ( i ) ; } } function printSum ( L , R ) { var sum = pref [ R ] - pref [ L - 1 ] ; document . write ( sum + " " ) ; } compute ( ) ; var Q = 4 ; var arr = [ [ 1 , 10 ] , [ 1 , 100 ] , [ 2 , 25 ] , [ 4 , 50 ] ] ; for ( var i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; }