Happy Number | Utility method to return sum of square of digit of n ; method return true if n is Happy number ; initialize slow and fast by n ; move slow number by one iteration ; move fast number by two iteration ; if both number meet at 1 , then return true ; Driver Code
def numSquareSum ( n ) : NEW_LINE INDENT squareSum = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT squareSum += ( n % 10 ) * ( n % 10 ) ; NEW_LINE n = int ( n / 10 ) ; NEW_LINE DEDENT return squareSum ; NEW_LINE DEDENT def isHappynumber ( n ) : NEW_LINE INDENT slow = n ; NEW_LINE fast = n ; NEW_LINE while ( True ) : NEW_LINE INDENT slow = numSquareSum ( slow ) ; NEW_LINE fast = numSquareSum ( numSquareSum ( fast ) ) ; NEW_LINE if ( slow != fast ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT return ( slow == 1 ) ; NEW_LINE DEDENT n = 13 ; NEW_LINE if ( isHappynumber ( n ) ) : NEW_LINE INDENT print ( n , " is ▁ a ▁ Happy ▁ number " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( n , " is ▁ not ▁ a ▁ Happy ▁ number " ) ; NEW_LINE DEDENT