Recursive sum of digit in n ^ x , where n and x are very large | function to get sum of digits of a number ; function to return sum ; Find sum of digits in n ; Find remainder of exponent ; Driver code
function digSum ( n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } function PowDigSum ( n , x ) { let sum = digSum ( n ) ; let rem = x % 6 ; if ( ( sum == 3 sum == 6 ) && x > 1 ) return 9 ; else if ( x == 1 ) return sum ; else if ( x == 0 ) return 1 ; else if ( rem == 0 ) return digSum ( Math . pow ( sum , 6 ) ) ; else return digSum ( Math . pow ( sum , rem ) ) ; } let n = 33333 ; let x = 332654 ; document . write ( PowDigSum ( n , x ) ) ;