Smallest number greater than or equal to N which is divisible by its non | Function to find the smallest number greater than or equal to N such that it is divisible by its non - zero digits ; Iterate in range [ N , N + 2520 ] ; To check if the current number satisfies the given condition ; Store the number in a temporary variable ; Loop until temp > 0 ; Check only for non zero digits ; Extract the current digit ; If number is divisible by current digit or not ; Otherwise , set possible to 0 ; Break out of the loop ; Divide by 10 ; Driver code ; Function Call
function findSmallestNumber ( n ) { for ( i = n ; i <= ( n + 2520 ) ; ++ i ) { var possible = 1 ; var temp = i ; while ( temp != 0 ) { if ( temp % 10 != 0 ) { var digit = temp % 10 ; if ( i % digit != 0 ) { possible = 0 ; break ; } } temp = parseInt ( temp / 10 ) ; } if ( possible == 1 ) { document . write ( i ) ; return ; } } } var N = 31 ; findSmallestNumber ( N ) ;