Check if N can be converted to the form K power K by the given operation | Javascript implementation to Check whether a given number N can be converted to the form K power K by the given operation ; Function to check if N can be converted to K power K ; Check if n is of the form k ^ k ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Return the result ; Function to check the above method ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7
var kPowKform = new Set ( ) ; function func ( n ) { if ( n <= 0 ) return 0 ; if ( kPowKform . has ( n ) ) return 1 ; var answer = 0 ; var x = n ; while ( x > 0 ) { var d = x % 10 ; if ( d != 0 ) { if ( func ( n - d * d ) ) { answer = 1 ; break ; } } x = parseInt ( x / 10 ) ; } return answer ; } function canBeConverted ( n ) { if ( func ( n ) ) document . write ( " " ) ; else document . write ( " " ) ; } var N = 90 ; for ( var i = 1 ; i <= 8 ; i ++ ) { var val = 1 ; for ( var j = 1 ; j <= i ; j ++ ) val *= i ; kPowKform . add ( val ) ; } canBeConverted ( N ) ;