Recursive sum of digit in n ^ x , where n and x are very large | CPP Code for Sum of digit of 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; long digSum ( long n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } long PowDigSum ( long n , long x ) { long sum = digSum ( n ) ; long 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 ( ( long ) pow ( sum , 6 ) ) ; else return digSum ( ( long ) pow ( sum , rem ) ) ; } int main ( ) { int n = 33333 ; int x = 332654 ; cout << PowDigSum ( n , x ) ; return 0 ; }