Digital Root of a given large integer using Recursion | Function to convert given sum into string ; Loop to extract digit one by one from the given sum and concatenate into the string ; Type casting for concatenation ; Return converted string ; Function to get individual digit sum from string ; Loop to get individual digit sum ; Function call to convert sum into string ; Function to calculate the digital root of a very large number ; Base condition ; Function call to get individual digit sum ; Recursive function to get digital root of a very large number ; Driver code ; Function to print final digit
def convertToString ( sum ) : NEW_LINE INDENT str1 = " " NEW_LINE while ( sum ) : NEW_LINE INDENT str1 = str1 + chr ( ( sum % 10 ) + ord ( '0' ) ) NEW_LINE sum = sum // 10 NEW_LINE DEDENT return str1 NEW_LINE DEDENT def GetIndividulaDigitSum ( str1 , len1 ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( len1 ) : NEW_LINE INDENT sum = sum + ord ( str1 [ i ] ) - ord ( '0' ) NEW_LINE DEDENT return convertToString ( sum ) NEW_LINE DEDENT def GetDigitalRoot ( str1 ) : NEW_LINE INDENT if ( len ( str1 ) == 1 ) : NEW_LINE INDENT return ord ( str1 [ 0 ] ) - ord ( '0' ) NEW_LINE DEDENT str1 = GetIndividulaDigitSum ( str1 , len ( str1 ) ) NEW_LINE return GetDigitalRoot ( str1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = "675987890789756545689070986776987" NEW_LINE print ( GetDigitalRoot ( str1 ) ) NEW_LINE DEDENT