Count of numbers in range [ L , R ] which can be represented as sum of two perfect powers | Function to find the number of numbers that can be expressed in the form of the sum of two perfect powers ; Stores all possible powers ; Push 1 and 0 in it ; Iterate over all the exponents ; Iterate over all possible numbers ; This loop will run for a maximum of sqrt ( R ) times ; Push this power in the array pows [ ] ; Increase the number ; Stores if i can be expressed as the sum of perfect power or not ; Iterate over all possible pairs of the array pows [ ] ; The number is valid ; Find the prefix sum of the array ok [ ] ; Return the count of required number ; Driver Code
function TotalPerfectPowerSum ( L , R ) { let pows = [ ] ; pows . push ( 0 ) ; pows . push ( 1 ) ; for ( let p = 2 ; p < 25 ; p ++ ) { let num = 2 ; while ( Math . floor ( Math . pow ( num , p ) + 0.5 ) <= R ) { pows . push ( Math . floor ( Math . pow ( num , p ) + 0.5 ) ) ; num ++ ; } } let ok = new Array ( R + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < pows . length ; i ++ ) { for ( let j = 0 ; j < pows . length ; j ++ ) { if ( pows [ i ] + pows [ j ] <= R && pows [ i ] + pows [ j ] >= L ) { ok [ pows [ i ] + pows [ j ] ] = 1 ; } } } for ( let i = 1 ; i <= R ; i ++ ) { ok [ i ] += ok [ i - 1 ] ; } return ok [ R ] - ok [ L - 1 ] ; } let L = 5 , R = 8 ; document . write ( TotalPerfectPowerSum ( L , R ) ) ;