human
Maximum of sum and product of digits until number is reduced to a single digit | Function to sum the digits until it becomes a single digit.
Function to product the digits until it becomes a single digit.
Loop to do sum while sum is not less than or equal to 9.
Function to find the maximum among repeated sum and repeated product.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to maximum of sum and product of digits until number is reduced to a single digit and the other steps required.
 def repeatedSum ( n ) :
	if ( n == 0 ) :
		return 0
	return 9 if ( n % 9 == 0 ) else ( n % 9 )
def repeatedProduct ( n ) :
	prod = 1
	while ( n > 0 or prod > 9 ) :
		if ( n == 0 ) :
			n = prod
			prod = 1
		prod *= n % 10
		n //= 10
	return prod
def maxSumProduct ( N ) :
	if ( N < 10 ) :
		return N
	return max ( repeatedSum ( N ) , repeatedProduct ( N ) )
if __name__ == " _ _ main _ _ " :
	n = 631
	print ( maxSumProduct ( n ) )