5 Best Ways to Find the Nearest Number with All Odd Digits in Python

Rate this post

πŸ’‘ Problem Formulation: The task is to write a Python program to find the nearest number to a given number, ‘n’, such that all digits in the resulting number are odd. For instance, if the input ‘n’ is 23, the nearest numbers with all odd digits would be 21 and 25; however, the closest is 21 since it is the least distance away from 23.

Method 1: Iterative Search

This method involves two separate sequences – one incrementally upwards from ‘n’ and the other decrementally downwards – to find the nearest number where all the digits are odd. The search stops as soon as the first such number is found in one of the directions. This approach is direct and straightforward in implementation.

Here’s an example:

def nearest_odd_number(n):
    def is_all_odd(num):
        return all(int(digit) % 2 != 0 for digit in str(num))

    up, down = n + 1, n - 1
    while True:
        if is_all_odd(up):
            return up
        if is_all_odd(down):
            return down
        up += 1
        down -= 1


The output of this code snippet is:


This code checks numbers incrementally up and down from ‘n’ using a while loop. The nested function ‘is_all_odd()’ returns ‘True’ if all the digits in a number are odd. Once an odd-digit number is found, it returns that number as soon as the condition is met.

Method 2: String Manipulation

By converting the number to a string, we can sequentially replace even digits from the rightmost side, checking for the nearest odd-digit number. This method can efficiently locate the closest odd-digit number by avoiding unnecessary checks on all numbers around ‘n’.

Here’s an example:

def nearest_odd_via_string(n):
    n_str = str(n)
    for i in range(len(n_str)-1, -1, -1):
        if int(n_str[i]) % 2 == 0:
            n_str = n_str[:i] + str((int(n_str[i]) - 1) % 10) + '9' * (len(n_str)-i-1)
    return int(n_str)


The output of this code snippet is:


This snippet iterates over the digits of ‘n’ in reverse order. When an even digit is encountered, it is decreased by one (and if it is ‘0’, it wraps to ‘9’), and all digits to the right are set to ‘9’. The number is then converted back to an integer for the result.

Method 3: Mathematical Approach

The mathematical approach involves subtracting a calculated value from ‘n’ to quickly determine the nearest number with all odd digits. It leverages division and modulus operations to isolate even digits and adjust the number accordingly.

Here’s an example:

def nearest_odd_mathematically(n):
    adjustment, place = 0, 0
    while n > 0:
        digit = n % 10
        if digit % 2 == 0:
            adjustment += (1 * (10 ** place))
        n //= 10
        place += 1
    return n - adjustment


The output of this code snippet is:


This function repeatedly isolates the rightmost digit of ‘n’ and checks if it is even. If it is, the function calculates the necessary adjustment for that place value. The value increments by powers of ten with an incrementing place counter as it moves left to right. Finally, the adjustment is subtracted from ‘n’ to find the nearest number with odd digits.

Method 4: Binary Search

Binary search isn’t typically associated with this problem, but we can apply a modified logic. By checking mid-points between a lower and higher bound (initialized to just below and just above ‘n’), we can home in on the nearest number with all odd digits using less computational steps compared to iterative methods.

Here’s an example:

def nearest_odd_binary_search(n):
    # Assume additional implementation for a binary search approach

# Due to the complexity and potential difficulty in implementing
# a binary search for this odd-specific context, we leave it 
# as an exercise to those interested in exploring it further.

This code snippet would outline a more advanced technique using binary search to check mid-points within a defined range. Since the execution is complex and goes beyond typical binary search logic, the details are left for the reader to explore.

Bonus One-Liner Method 5: Using List Comprehensions

This one-liner is for the Python enthusiasts who love condensed code. It uses a list comprehension along with the min function. The code finds all odd-digit numbers within a specific range and then selects the closest one to ‘n’.

Here’s an example:

nearest_odd_oneliner = lambda n: min((i for i in range(n-10, n+10) if all(int(d)%2 for d in str(i))), key=lambda x: abs(x-n))


The output of this code snippet is:


This one-liner defines a lambda function that generates a range of numbers around ‘n’, filters out the numbers with all odd digits, and then selects the one whose absolute difference from ‘n’ is minimal.


  • Method 1: Iterative Search. This approach is easy to understand and implement. It can, however, be inefficient for large numbers as it checks every number sequentially.
  • Method 2: String Manipulation. More efficient than the iterative approach, it can provide a quick answer without checking each number. The downside might be that string operations could be relatively slower for some implementations.
  • Method 3: Mathematical Approach. It may be the fastest for digits nearer to the least significant digit but becomes complex as more digits are involved, making it less intuitive.
  • Method 4: Binary Search. Would potentially be very efficient with the correct implementation but presents significant challenges in logic due to variable gap sizes between odd-digit numbers.
  • Method 5: Using List Comprehensions. Offers a concise and Pythonic solution but may not be as efficient or readable for those new to such expressions.