Find Nth positive number whose digital root is X | Python3 program to find the N - th number whose digital root is X ; Function to find the digital root of a number ; Function to find the Nth number with digital root as X ; Counter variable to keep the count of valid numbers ; Find digital root ; Check if is required answer or not ; Print the answer if you have found it and breakout of the loop ; Driver Code
import sys NEW_LINE def findDigitalRoot ( num ) : NEW_LINE INDENT sum = sys . maxsize ; NEW_LINE tempNum = num ; NEW_LINE while ( sum >= 10 ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( tempNum > 0 ) : NEW_LINE INDENT sum += tempNum % 10 ; NEW_LINE tempNum //= 10 ; NEW_LINE DEDENT tempNum = sum ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def findAnswer ( X , N ) : NEW_LINE INDENT counter = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( counter < N ) : NEW_LINE INDENT i += 1 ; NEW_LINE digitalRoot = findDigitalRoot ( i ) ; NEW_LINE if ( digitalRoot == X ) : NEW_LINE INDENT counter += 1 ; NEW_LINE DEDENT if ( counter == N ) : NEW_LINE INDENT print ( i ) ; NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 1 ; NEW_LINE N = 3 ; NEW_LINE findAnswer ( X , N ) ; NEW_LINE DEDENT