Check if a number can be expressed as sum of two Perfect powers | Function that returns true if n can be written as a ^ m + b ^ n ; Taking isSum boolean array for check the sum exist or not ; To store perfect squares ; Initially all sums as false ; If sum exist then push that sum leto perfect square vector ; Mark all perfect powers as false ; Traverse each perfectPowers ; Calculating Sum with perfect powers array ; Given number n ; Function call
function isSumOfPower ( n ) { let isSum = Array ( n + 1 ) . fill ( 0 ) ; let perfectPowers = [ ] ; perfectPowers . push ( 1 ) ; for ( let i = 0 ; i < ( n + 1 ) ; i ++ ) { isSum [ i ] = false ; } for ( let i = 2 ; i < ( n + 1 ) ; i ++ ) { if ( isSum [ i ] == true ) { perfectPowers . push ( i ) ; continue ; } for ( let j = i * i ; j > 0 && j < ( n + 1 ) ; j *= i ) { isSum [ j ] = true ; } } for ( let i = 0 ; i < perfectPowers . length ; i ++ ) { isSum [ perfectPowers [ i ] ] = false ; } for ( let i = 0 ; i < perfectPowers . length ; i ++ ) { for ( let j = i ; j < perfectPowers . length ; j ++ ) { let sum = perfectPowers [ i ] + perfectPowers [ j ] ; if ( sum < ( n + 1 ) ) isSum [ sum ] = true ; } } return isSum [ n ] ; } let n = 9 ; if ( isSumOfPower ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }