Project Euler #145: How many reversible numbers are there below one-billion?

  • + 0 comments

    Time Out Error

    def is_reversible(n):
        if n % 10 == 0:  # Avoid leading zeroes in the reverse
            return False
        
        sum_n_reverse = n + int(str(n)[::-1])  # Sum of n and its reverse
        for digit in str(sum_n_reverse):
            if int(digit) % 2 == 0:  # Check for even digits in the sum
                return False
        return True
    
    def count_reversible_numbers(limit):
        count = 0
        for i in range(1, limit):
            if is_reversible(i):
                count += 1
        return count
    
    def main():
        test_cases = int(input())
        for _ in range(test_cases):
            limit = int(input())
            reversible_count = count_reversible_numbers(limit)
            print(reversible_count)
    
    if __name__ == "__main__":
        main()