Trimorphic Number | JavaScript program for the above approach ; Functions to find nth Trimorphic number ; Comparing the digits ; Return false , if any digit of num doesn ' t ▁ ▁ match ▁ with ▁ its ▁ cube ' s digits from last ; Reduce num and cube ; Check in max let size ; check number is Trimorphic or not ; if counter is equal to the n then return nth number ; Driver Code
let LET_MAX = 2147483647 ; function checkTrimorphic ( num ) { let cube = num * num * num ; while ( num > 0 ) { if ( num % 10 != cube % 10 ) return false ; num = Math . floor ( num / 10 ) ; cube = Math . floor ( cube / 10 ) ; } return true ; } function nthTrimorphic ( n ) { let count = 0 ; for ( let i = 0 ; i < LET_MAX ; i ++ ) { if ( checkTrimorphic ( i ) ) count ++ ; if ( count == n ) return i ; } return - 1 ; } let n = 9 ; document . write ( nthTrimorphic ( n ) ) ;