2

OEIS A036301 is a sequence of numbers $ n $ such that the sum of the even digits of $ n $ equals the sum of the odd digits of $ n $. Let's call these numbers "balanced". Given an input positive integer $ k $, I would like a procedure that finds the nearest balanced integer. Occasionally, $ k $ might be equidistant from two balanced integers, in which case the procedure should output both. For a relatively small $ k $, this problem is not too difficult to brute-force. But for a large $ k $, if there is a great disparity between the sum of the even digits and the sum of the odd digits, brute force takes too long.

For example, if $ k = 1037708753757778746280242 $, the nearest balanced integer appears to be $ 1037708753757778800288888 $. How do we quickly determine this without checking all integers from $ k - 54008646 $ to $ k + 54008646 $?

  • 2
    Hint: define the imbalance of an integer as the sum of its even digits minus the sum of its odd digits. Then the imbalances of the prefixes of $k$ are -1, -1, -4, -11, -18, -18, -10, -17, -22, -25, -32, -37, -44, -51, -58, -50, -57, -53, -47, -45, -37, -37, -35, -31, -29. – Peter Taylor Dec 11 '18 at 15:22
  • @PeterTaylor - i am not sure why we need the imbalances of all the prefixes. why not just calculate the total imbalance, then work from the rightmost digit and keep changing them until the imbalance becomes 0? would that work? (or is that actually equiv. to what you have in mind?) – antkam Dec 12 '18 at 15:17

0 Answers0