Returns single digit sum of a ^ n . We use modular exponentiation technique .
def powerDigitSum ( a , n ) : NEW_LINE INDENT res = 1 NEW_LINE while ( n ) : NEW_LINE INDENT if n % 2 == 1 : NEW_LINE INDENT res = res * digSum ( a ) NEW_LINE res = digSum ( res ) NEW_LINE DEDENT a = digSum ( digSum ( a ) * digSum ( a ) ) NEW_LINE n //= 2 NEW_LINE DEDENT return res NEW_LINE DEDENT