Find the greater number closest to N having at most one non | Python3 program to implement the above approach ; Function to calculate X ^ n in log ( n ) ; Stores the value of X ^ n ; If N is odd ; Function to find the closest number > N having at most 1 non - zero digit ; Stores the count of digits in N ; Stores the power of 10 ^ ( n - 1 ) ; Stores the last ( n - 1 ) digits ; Store the answer ; Driver Code ; Function call
import math NEW_LINE def power ( X , n ) : NEW_LINE INDENT res = 1 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT if ( n & 1 != 0 ) : NEW_LINE INDENT res = res * X NEW_LINE DEDENT X = X * X NEW_LINE n = n >> 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def closestgtNum ( N ) : NEW_LINE INDENT n = int ( math . log10 ( N ) + 1 ) NEW_LINE P = power ( 10 , n - 1 ) NEW_LINE Y = N % P NEW_LINE res = N + ( P - Y ) NEW_LINE return res NEW_LINE DEDENT N = 120 NEW_LINE print ( closestgtNum ( N ) ) NEW_LINE