Some positive integers $n$ have the property that the sum $[n + \operatorname{reverse}(n)]$ consists entirely of odd (decimal) digits. For instance, $36 + 63 = 99$ and $409 + 904 = 1313$. We will call such numbers reversible; so $36$, $63$, $409$, and $904$ are reversible. Leading zeroes are not allowed in either $n$ or $\operatorname{reverse}(n)$.
There are $120$ reversible numbers below one-thousand.
How many reversible numbers are there below one-billion ($10^9$)?
To solve this problem, we can iterate through all numbers less than one billion and check if their sum with their reverse contains only odd digits. We need to exclude leading zeros in both $n$ and reverse(n).
Here is a Python code that solves this problem:
“`python
def reverse_num(n):
# Function to reverse a number
reverse = 0
while n > 0:
reverse = reverse * 10 + n % 10
n //= 10
return reverse
count = 0
limit = 10**9
for n in range(1, limit):
if n % 10 == 0: # Exclude numbers ending with 0
continue
reverse = reverse_num(n)
if reverse % 2 == 0: # Exclude numbers with even reverse
continue
if (n + reverse) % 2 == 0: # Exclude numbers with even sum
continue
count += 1
print(“Number of reversible numbers below one-billion:”, count)
“`
This code defines a function called `reverse_num` that takes an integer `n` and returns its reverse. The main logic of the code is in the for loop, where we iterate through all numbers less than one billion.
Inside the loop, we check if the number ends with zero. If it does, we skip to the next iteration to exclude numbers with leading zeros. Then, we calculate the reverse of the number using the `reverse_num` function and check if it is odd. If the reverse is even, we skip to the next iteration.
Finally, we check if the sum of `n` and its reverse is odd. If it is even, we skip to the next iteration. If none of the exclusion conditions are met, we increment the count.
At the end of the loop, we print the final count, which represents the number of reversible numbers below one billion.
Note: This code may take some time to execute since it iterates through a large number of values. You can also optimize the code further by observing the pattern in reversible numbers.
More Answers:
Perfect Square CollectionTorricelli Triangles
Laser Beam Reflections